@pnp/spfx-property-controls
Version:
Reusable property pane controls for SharePoint Framework solutions
113 lines • 7.12 kB
JavaScript
import { __awaiter } from "tslib";
import { FileBrowserService } from "./FileBrowserService";
import { SPHttpClient } from "@microsoft/sp-http";
export class OrgAssetsService extends FileBrowserService {
constructor(context, itemsToDownloadCount) {
super(context, itemsToDownloadCount);
this._orgAssetsLibraryServerRelativeSiteUrl = null;
this.getListItems = (listUrl, folderPath, acceptedFilesExtensions, nextPageQueryStringParams) => __awaiter(this, void 0, void 0, function* () {
let filesQueryResult = { items: [], nextHref: null };
try {
// Retrieve Lib path from folder path
if (folderPath.charAt(0) !== "/") {
folderPath = `/${folderPath}`;
}
// Remove all the rest of the folder path
let libName = folderPath.replace(`${this._orgAssetsLibraryServerRelativeSiteUrl}/`, "");
libName = libName.split("/")[0];
// Buil absolute library URL
const libFullUrl = this.buildAbsoluteUrl(`${this._orgAssetsLibraryServerRelativeSiteUrl}/${libName}`);
let queryStringParams = "";
// Do not pass FolderServerRelativeUrl as query parameter
// Attach passed nextPageQueryStringParams values to REST URL
if (nextPageQueryStringParams) {
// Remove start ? from the query params
if (nextPageQueryStringParams.charAt(0) === "?") {
nextPageQueryStringParams = nextPageQueryStringParams.substring(1);
}
queryStringParams = nextPageQueryStringParams;
}
else {
queryStringParams = `RootFolder=${folderPath}`;
}
const restApi = `${this.context.pageContext.web.absoluteUrl}/_api/SP.List.GetListDataAsStream?listFullUrl='${libFullUrl}'&${queryStringParams}`;
filesQueryResult = yield this._getListDataAsStream(restApi, null, acceptedFilesExtensions);
}
catch (error) {
filesQueryResult.items = null;
console.error(error instanceof Error ? error.message : String(error));
}
return filesQueryResult;
});
this.getListItemsByListId = (listId, folderPath, acceptedFilesExtensions, nextPageQueryStringParams) => __awaiter(this, void 0, void 0, function* () {
let filesQueryResult = { items: [], nextHref: null };
try {
let orgAssetLibraryServerRelativeUrlWithoutTrailingSlash = this._orgAssetsLibraryServerRelativeSiteUrl;
if (orgAssetLibraryServerRelativeUrlWithoutTrailingSlash.charAt(orgAssetLibraryServerRelativeUrlWithoutTrailingSlash.length - 1) === '/') {
orgAssetLibraryServerRelativeUrlWithoutTrailingSlash = orgAssetLibraryServerRelativeUrlWithoutTrailingSlash.slice(0, -1);
}
// Retrieve Lib path from folder path
if (folderPath.charAt(0) !== "/") {
folderPath = `/${folderPath}`;
}
// Remove all the rest of the folder path
let libName = folderPath.replace(`${orgAssetLibraryServerRelativeUrlWithoutTrailingSlash}/`, "");
libName = libName.split("/")[0]; // Get only library name, if navigated to sub folder in the picker
// Build absolute library URL
const libFullUrl = this.buildAbsoluteUrl(`${orgAssetLibraryServerRelativeUrlWithoutTrailingSlash}/${libName}`);
let queryStringParams = "";
// Do not pass FolderServerRelativeUrl as query parameter
// Attach passed nextPageQueryStringParams values to REST URL
if (nextPageQueryStringParams) {
// Remove start ? from the query params
if (nextPageQueryStringParams.charAt(0) === "?") {
nextPageQueryStringParams = nextPageQueryStringParams.substring(1);
}
queryStringParams = nextPageQueryStringParams;
}
else {
queryStringParams = `RootFolder=${folderPath}`;
}
const restApi = `${this.context.pageContext.web.absoluteUrl}/_api/SP.List.GetListDataAsStream?listFullUrl='${libFullUrl}'&${queryStringParams}`;
filesQueryResult = yield this._getListDataAsStream(restApi, null, acceptedFilesExtensions);
}
catch (error) {
filesQueryResult.items = null;
console.error(error instanceof Error ? error.message : String(error));
}
return filesQueryResult;
});
this.getSiteMediaLibraries = (...args_1) => __awaiter(this, [...args_1], void 0, function* (includePageLibraries = false) {
try {
const restApi = `${this.context.pageContext.web.absoluteUrl}/_api/SP.Publishing.SitePageService.FilePickerTabOptions`;
const orgAssetsResult = yield this.context.spHttpClient.get(restApi, SPHttpClient.configurations.v1);
if (!orgAssetsResult || !orgAssetsResult.ok) {
throw new Error(`Something went wrong when executing request. Status='${orgAssetsResult.status}'`);
}
const orgAssetsData = yield orgAssetsResult.json();
if (!orgAssetsData || !orgAssetsData.OrgAssets || !orgAssetsData.OrgAssets.OrgAssetsLibraries || !orgAssetsData.OrgAssets.OrgAssetsLibraries.Items || orgAssetsData.OrgAssets.OrgAssetsLibraries.Items.length <= 0) {
return null;
}
this._orgAssetsLibraryServerRelativeSiteUrl = orgAssetsData ? orgAssetsData.OrgAssets.Url.DecodedUrl : null;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const libs = orgAssetsData && orgAssetsData.OrgAssets ? orgAssetsData.OrgAssets.OrgAssetsLibraries.Items.map((libItem) => { return this._parseOrgAssetsLibraryItem(libItem); }) : [];
return libs;
}
catch (error) {
console.error(`[OrgAssetsService.getOrganisationAssetsLibraries]: Err='${error instanceof Error ? error.message : String(error)}'`);
return null;
}
});
this._parseOrgAssetsLibraryItem = (libItem) => {
const orgAssetsLibrary = {
absoluteUrl: this.buildAbsoluteUrl(`/${libItem.LibraryUrl.DecodedUrl}`),
title: libItem.DisplayName,
id: libItem.ListId,
serverRelativeUrl: libItem.LibraryUrl.DecodedUrl,
iconPath: libItem.ThumbnailUrl && libItem.ThumbnailUrl.DecodedUrl ? this.buildAbsoluteUrl(`${this._orgAssetsLibraryServerRelativeSiteUrl}/${libItem.ThumbnailUrl.DecodedUrl}`) : null
};
return orgAssetsLibrary;
};
}
}
//# sourceMappingURL=OrgAssetsService.js.map