@pnp/spfx-property-controls
Version:
Reusable property pane controls for SharePoint Framework solutions
88 lines • 4.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FolderExplorerService = void 0;
const tslib_1 = require("tslib");
const sp_http_1 = require("@microsoft/sp-http");
class FolderExplorerService {
constructor(context) {
/**
* Get libraries within a given site
* @param webAbsoluteUrl - the url of the target site
*/
this.getDocumentLibraries = (webAbsoluteUrl) => tslib_1.__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, sp_http_1.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) => tslib_1.__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, sp_http_1.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) => tslib_1.__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, sp_http_1.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;
}
}
exports.FolderExplorerService = FolderExplorerService;
//# sourceMappingURL=FolderExplorerService.js.map