Skip to content

Instantly share code, notes, and snippets.

@ypcode
Last active January 25, 2019 10:41
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/bcacc768398a2f6e06756d712ddd4de7 to your computer and use it in GitHub Desktop.
Save ypcode/bcacc768398a2f6e06756d712ddd4de7 to your computer and use it in GitHub Desktop.
A service for fetching basic SharePoint list information
import { ServiceScope, ServiceKey } from "@microsoft/sp-core-library";
import { SPHttpClient } from "@microsoft/sp-http";
/**
* The List service contract
*/
export interface IListService {
configure(webUrl: string, listId: string);
getListData(): Promise<IListData>;
}
export interface IListData {
Title: string;
ItemsCount: number;
}
const SERVICE_KEY_TOKEN = "ListService";
/**
* List service default implementation
*/
export class ListService implements IListService {
private _listId: string;
private _webUrl: string;
constructor(private serviceScope: ServiceScope) {}
/**
* Set the configuration of the service
* @param webUrl The URL of the SharePoint web
* @param listId THe ID of the list to work on
*/
public configure(webUrl: string, listId: string) {
this._webUrl = webUrl;
this._listId = listId;
}
/**
* Gets basic information about the configured SharePoint list
*/
public getListData(): Promise<IListData> {
const apiUrl = `${this._webUrl}/_api/web/lists(guid'${this._listId}')?$select=Title,ItemCount`;
const client = this.serviceScope.consume(SPHttpClient.serviceKey);
return client.get(apiUrl, SPHttpClient.configurations.v1)
.then(r => r.json())
.then(r => ({
Title: r.Title,
ItemsCount: r.ItemCount
} as IListData));
}
public static serviceKey: ServiceKey<IListService> = ServiceKey.create(SERVICE_KEY_TOKEN, ListService);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment