-
-
Save ypcode/bcacc768398a2f6e06756d712ddd4de7 to your computer and use it in GitHub Desktop.
A service for fetching basic SharePoint list information
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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