From e78b87d80aa7f573a0a2e8ad684414ae8416a507 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Lind=C3=A9n?= Date: Sun, 18 Feb 2024 18:29:13 +0100 Subject: [PATCH] Fix root site org asset library showing no items in file picker - When the file picker is used to pick files from an organizational asset library on the SharePoint root site, no files are shown. - Also fix an error in the _parseOrgAssetsLibraryItem function, which causes the absoluteUrl property to lack one / after the root SharePoint URL (e.g. "https://TENANT.sharepoint.comOrganizational Media Library" or "https://TENANT.sharepoint.comsites/OrgAssetsSite/Branding images"). Since this is faulty for org asset libraries on ALL sites, I don't think this property is used, otherwise it would have caused problems already. Explanation of error in file picker of organizational asset library: 1. The relative url of the root site is /, which causes a / to still be present in the library name, after removal of the site relative url. 2. This is turn causes the library name to be an empty string after splitting the library name on /. 3. When building the API request URL for retrieving the organizational assets library items, the library name is therefore missing from the listFullUrl query parameter. - In addition to this, the listFullUrl query parameter can't contain two slashes in front of the library name (also causes the request to fail). - This error would also happen if the site server relative url (_orgAssetsLibraryServerRelativeSiteUrl) starts with a slash. --- src/services/OrgAssetsService.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/services/OrgAssetsService.ts b/src/services/OrgAssetsService.ts index d207f6da..d5685d32 100644 --- a/src/services/OrgAssetsService.ts +++ b/src/services/OrgAssetsService.ts @@ -48,15 +48,20 @@ export class OrgAssetsService extends FileBrowserService { public getListItemsByListId = async (listId: string, folderPath: string, acceptedFilesExtensions?: string[], nextPageQueryStringParams?: string): Promise => { let filesQueryResult: 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(`${this._orgAssetsLibraryServerRelativeSiteUrl}/`, ""); - libName = libName.split("/")[0]; - // Buil absolute library URL - const libFullUrl = this.buildAbsoluteUrl(`${this._orgAssetsLibraryServerRelativeSiteUrl}/${libName}`); + 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: string = ""; // Do not pass FolderServerRelativeUrl as query parameter @@ -104,7 +109,7 @@ export class OrgAssetsService extends FileBrowserService { private _parseOrgAssetsLibraryItem = (libItem: any): ILibrary => { // eslint-disable-line @typescript-eslint/no-explicit-any const orgAssetsLibrary: ILibrary = { - absoluteUrl: this.buildAbsoluteUrl(libItem.LibraryUrl.DecodedUrl), + absoluteUrl: this.buildAbsoluteUrl(`/${libItem.LibraryUrl.DecodedUrl}`), title: libItem.DisplayName, id: libItem.ListId, serverRelativeUrl: libItem.LibraryUrl.DecodedUrl,