UNPKG

@pnp/spfx-property-controls

Version:

Reusable property pane controls for SharePoint Framework solutions

107 lines 4.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const sp_http_1 = require("@microsoft/sp-http"); const IPropertyFieldListPicker_1 = require("../propertyFields/listPicker/IPropertyFieldListPicker"); /** * Service implementation to get list & list items from current SharePoint site */ class SPListPickerService { /** * Service constructor */ constructor(_props, pageContext) { this.props = _props; this.context = pageContext; } /** * Gets the collection of libs in the current SharePoint site, or target site if specified by webRelativeUrl */ getLibs() { return tslib_1.__awaiter(this, void 0, void 0, function* () { // use the web relative url if provided, otherwise default to current SharePoint site const webAbsoluteUrl = this.props.webAbsoluteUrl ? this.props.webAbsoluteUrl : this.context.pageContext.web.absoluteUrl; // If the running environment is SharePoint, request the lists REST service let queryUrl; if (this.props.contentTypeId) { queryUrl = `${webAbsoluteUrl}/_api/lists?$select=Title,id,BaseTemplate,RootFolder/ServerRelativeUrl,ContentTypes/StringId,ContentTypes/Name&$expand=RootFolder&$expand=ContentTypes`; } else { queryUrl = `${webAbsoluteUrl}/_api/lists?$select=Title,id,BaseTemplate,RootFolder/ServerRelativeUrl&$expand=RootFolder`; } // Check if the orderBy property is provided if (this.props.orderBy !== null) { queryUrl += '&$orderby='; switch (this.props.orderBy) { case IPropertyFieldListPicker_1.PropertyFieldListPickerOrderBy.Id: queryUrl += 'Id'; break; case IPropertyFieldListPicker_1.PropertyFieldListPickerOrderBy.Title: queryUrl += 'Title'; break; } } // Adds an OData Filter to the list if (this.props.filter) { queryUrl += `&$filter=${encodeURIComponent(this.props.filter)}`; } // Check if the list have get filtered based on the list base template type else if ((this.props.baseTemplate !== null && this.props.baseTemplate) || Array.isArray(this.props.baseTemplate)) { if (Array.isArray(this.props.baseTemplate)) { queryUrl += '&$filter=('; queryUrl += this.props.baseTemplate.map(temp => `(BaseTemplate%20eq%20${temp})`).join('%20or%20'); queryUrl += ')'; } else { queryUrl += '&$filter=BaseTemplate%20eq%20'; queryUrl += this.props.baseTemplate; } // Check if you also want to exclude hidden list in the list if (this.props.includeHidden === false) { queryUrl += '%20and%20Hidden%20eq%20false'; } } else { if (this.props.includeHidden === false) { queryUrl += '&$filter=Hidden%20eq%20false'; } } const response = yield this.context.spHttpClient.get(queryUrl, sp_http_1.SPHttpClient.configurations.v1); let lists = (yield response.json()); //remove unwanted contenttypes if (this.props.contentTypeId) { const testct = this.props.contentTypeId.toUpperCase(); lists.value = lists.value.filter((l) => { for (const ct of l.ContentTypes) { const ctid = ct.StringId.toUpperCase(); if (ctid.substring(0, testct.length) === testct) { return true; } } return false; }); } // Check if onListsRetrieved callback is defined if (this.props.onListsRetrieved) { //Call onListsRetrieved const lr = this.props.onListsRetrieved(lists.value); let output; //Conditional checking to see of PromiseLike object or array if (lr instanceof Array) { output = lr; } else { output = yield lr; } lists = { value: output, }; } return lists; }); } } exports.default = SPListPickerService; //# sourceMappingURL=SPListPickerService.js.map