UNPKG

@pnp/spfx-property-controls

Version:

Reusable property pane controls for SharePoint Framework solutions

84 lines 4.32 kB
import { __awaiter } from "tslib"; import { SPHttpClient } from "@microsoft/sp-http"; export class FolderExplorerService { constructor(context) { /** * Get libraries within a given site * @param webAbsoluteUrl - the url of the target site */ this.getDocumentLibraries = (webAbsoluteUrl) => __awaiter(this, void 0, void 0, function* () { let results = []; try { const url = `${webAbsoluteUrl}/_api/web/lists?$filter=BaseTemplate eq 101 and Hidden eq false&$expand=RootFolder&$select=Title,RootFolder/ServerRelativeUrl&$orderby=Title`; const response = yield this.context.spHttpClient.get(url, SPHttpClient.configurations.v1); if (!response.ok) { throw new Error(`Something went wrong when retrieving libraries. Status='${response.status}'`); } const data = yield response.json(); // eslint-disable-next-line @typescript-eslint/no-explicit-any const libraries = data.value; results = libraries.map((library) => { return { Name: library.Title, ServerRelativeUrl: library.RootFolder.ServerRelativeUrl }; }); } catch (error) { console.error('Error loading libraries', error); } return results; }); /** * Get folders within a given library or sub folder * @param webAbsoluteUrl - the url of the target site * @param folderRelativeUrl - the relative url of the folder */ this.getFolders = (webAbsoluteUrl, folderRelativeUrl) => __awaiter(this, void 0, void 0, function* () { let results = []; try { const escapedPath = folderRelativeUrl.replace(/'/g, "''"); const url = `${webAbsoluteUrl}/_api/web/GetFolderByServerRelativePath(decodedUrl='${escapedPath}')/Folders?$select=Name,ServerRelativeUrl&$orderby=Name`; const response = yield this.context.spHttpClient.get(url, SPHttpClient.configurations.v1); if (!response.ok) { throw new Error(`Something went wrong when retrieving folders. Status='${response.status}'`); } const data = yield response.json(); const foldersResult = data.value; results = foldersResult.filter(f => f.Name !== "Forms"); } catch (error) { console.error('Error loading folders', error); } return results; }); /** * Create a new folder * @param webAbsoluteUrl - the url of the target site * @param folderRelativeUrl - the relative url of the base folder * @param name - the name of the folder to be created */ this.addFolder = (webAbsoluteUrl, folderRelativeUrl, name) => __awaiter(this, void 0, void 0, function* () { let folder = null; try { // Escape single quotes for the path parameter, but don't encode the slashes const escapedPath = folderRelativeUrl.replace(/'/g, "''"); // For the folder name, escape single quotes const escapedName = name.replace(/'/g, "''"); const url = `${webAbsoluteUrl}/_api/web/GetFolderByServerRelativePath(decodedUrl='${escapedPath}')/AddSubFolderUsingPath(decodedUrl='${escapedName}')`; const response = yield this.context.spHttpClient.post(url, SPHttpClient.configurations.v1, {}); if (!response.ok) { throw new Error(`Something went wrong when adding folder. Status='${response.status}'`); } // AddSubFolderUsingPath returns 204 No Content on success (we could requery to get exact values if needed) folder = { Name: name, ServerRelativeUrl: `${folderRelativeUrl}/${name}` }; } catch (error) { console.error('Error adding folder', error); } return folder; }); this.context = context; } } //# sourceMappingURL=FolderExplorerService.js.map