Skip to content

Instantly share code, notes, and snippets.

@ypcode
Created August 19, 2019 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ypcode/3ca3816a0e7f8b5d169bd4396035c949 to your computer and use it in GitHub Desktop.
Save ypcode/3ca3816a0e7f8b5d169bd4396035c949 to your computer and use it in GitHub Desktop.
import { IList } from "../models/IList";
import { ServiceScope, ServiceKey } from "@microsoft/sp-core-library";
import { ComponentContextServiceKey } from "./ComponentContextService";
import { SPHttpClient } from "@microsoft/sp-http";
import { PageContextServiceKey } from "./PageContextService";
export interface IListService {
getListByTitle(listTitle: string): Promise<IList>;
}
export class ListService implements IListService {
constructor(private serviceScope: ServiceScope) {
}
public getListByTitle(listTitle: string): Promise<IList> {
return new Promise<IList>((resolve, reject) => {
// Ensure the service scope is completely configured before we can consume any service
this.serviceScope.whenFinished(() => {
const pageContext = this.serviceScope.consume(PageContextServiceKey);
const spHttpClient = this.serviceScope.consume(SPHttpClient.serviceKey);
const url = `${pageContext.webAbsoluteUrl}/_api/web/lists/getbytitle('${escape(listTitle)}')`;
spHttpClient.get(url, SPHttpClient.configurations.v1)
.then(r => r.json())
.then(l => {
resolve({
id: l.Id,
title: l.Title,
itemsCount: l.ItemCount
} as IList);
});
});
});
}
}
export const ListServiceKey = ServiceKey.create<IListService>("ypcode::ListService", ListService);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment