Skip to content

Instantly share code, notes, and snippets.

@ypcode
Created August 19, 2019 10:25
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/83aaeca5a8db8c090aea85864a86f709 to your computer and use it in GitHub Desktop.
Save ypcode/83aaeca5a8db8c090aea85864a86f709 to your computer and use it in GitHub Desktop.
import { ServiceKey, ServiceScope } from "@microsoft/sp-core-library";
import { ListServiceKey } from "./ListsService";
import { ComponentContextServiceKey } from "./ComponentContextService";
import { WebPartContext } from "@microsoft/sp-webpart-base";
export interface IDocumentsService {
getDocumentsCount(): Promise<number>;
}
export class DocumentsService implements IDocumentsService {
constructor(private serviceScope: ServiceScope) { }
public getDocumentsCount(): Promise<number> {
return new Promise<number>((resolve, reject) => {
// Ensure the service scope is completely configured before we can consume any service
this.serviceScope.whenFinished(() => {
const listService = this.serviceScope.consume(ListServiceKey);
const componentContextService = this.serviceScope.consume(ComponentContextServiceKey);
const docLibName = componentContextService.properties.documentLibraryName;
listService.getListByTitle(docLibName).then(list => {
resolve(list.itemsCount);
});
});
});
}
}
export const DocumentsServiceKey = ServiceKey.create<IDocumentsService>("ypcode::DocumentsService", DocumentsService);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment