From 99e15fd9c4edc89200af1f80d8430d9a7b47a90c Mon Sep 17 00:00:00 2001 From: Martin Lingstuyl Date: Tue, 19 Nov 2024 17:03:06 +0100 Subject: [PATCH] Be able to use a server relative folder path. --- .../docs/controls/DynamicForm.md | 2 +- src/controls/dynamicForm/DynamicForm.tsx | 22 ++++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/docs/documentation/docs/controls/DynamicForm.md b/docs/documentation/docs/controls/DynamicForm.md index 0e8f27972..1521fa379 100644 --- a/docs/documentation/docs/controls/DynamicForm.md +++ b/docs/documentation/docs/controls/DynamicForm.md @@ -66,7 +66,7 @@ The `DynamicForm` can be configured with the following properties: | validationErrorDialogProps | IValidationErrorDialogProps | no | Specifies validation error dialog properties | | customIcons | { [ columnInternalName: string ]: string } | no | Specifies custom icons for the form. The key of this dictionary is the column internal name, the value is the Fluent UI icon name. | | storeLastActiveTab | boolean | no | When uploading files: Specifies if last active tab will be stored after the Upload panel has been closed. Note: the value of selected tab is stored in the queryString hash. Default - `true` | -| folderPath | string | no | Library relative folder to create the item in. This option is only available for document libraries and works only when the contentTypeId is specified and has a base type of type Document or Folder. Defaults to the root folder. | +| folderPath | string | no | Server relative or library relative folder to create the item in. This option is only available for document libraries and works only when the contentTypeId is specified and has a base type of type Document or Folder. Defaults to the root folder of the library. | ## Validation Error Dialog Properties `IValidationErrorDialogProps` | Property | Type | Required | Description | diff --git a/src/controls/dynamicForm/DynamicForm.tsx b/src/controls/dynamicForm/DynamicForm.tsx index 8bcaac4ba..9714915d4 100644 --- a/src/controls/dynamicForm/DynamicForm.tsx +++ b/src/controls/dynamicForm/DynamicForm.tsx @@ -1478,10 +1478,26 @@ export class DynamicForm extends React.Component< return 'Document'; } } - - private getFolderByPath = async (listRelativeFolderPath: string, rootFolder: IFolder): Promise => { + + /** + * Returns a pnp/sp folder object based on the folderPath and the library the folder is in. + * The folderPath can be a server relative path, but should be in the same library. + * @param folderPath The path to the folder coming from the component properties + * @param rootFolder The rootFolder object of the library + * @returns + */ + private getFolderByPath = async (folderPath: string, rootFolder: IFolder): Promise => { const libraryFolder = await rootFolder(); - const folder = sp.web.getFolderByServerRelativePath(`${libraryFolder.ServerRelativeUrl}/${listRelativeFolderPath}`); + const normalizedFolderPath = decodeURIComponent(folderPath).toLowerCase().replace(/\/$/, ""); + const serverRelativeLibraryPath = libraryFolder.ServerRelativeUrl.toLowerCase().replace(/\/$/, ""); + + // In case of a server relative path in the same library, return the folder + if (`${normalizedFolderPath}/`.startsWith(`${serverRelativeLibraryPath}/`)) { + return sp.web.getFolderByServerRelativePath(normalizedFolderPath); + } + + // In other cases, expect a list-relative path and return the folder + const folder = sp.web.getFolderByServerRelativePath(`${serverRelativeLibraryPath}/${normalizedFolderPath}`); return folder; }; }