-
-
Save ypcode/3ca3816a0e7f8b5d169bd4396035c949 to your computer and use it in GitHub Desktop.
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 { 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