Skip to content

Commit 5eb14c9

Browse files
Adds ability to create files/folders in subfolder using DynamicForm. Closes #1901
1 parent 2bd431c commit 5eb14c9

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

docs/documentation/docs/controls/DynamicForm.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ The `DynamicForm` can be configured with the following properties:
6666
| validationErrorDialogProps | IValidationErrorDialogProps | no | Specifies validation error dialog properties |
6767
| 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. |
6868
| 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` |
69+
| 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. |
6970

7071
## Validation Error Dialog Properties `IValidationErrorDialogProps`
7172
| Property | Type | Required | Description |

src/controls/dynamicForm/DynamicForm.tsx

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import "@pnp/sp/lists";
3131
import "@pnp/sp/content-types";
3232
import "@pnp/sp/folders";
3333
import "@pnp/sp/items";
34+
import { IFolder } from "@pnp/sp/folders";
3435
import { IInstalledLanguageInfo } from "@pnp/sp/presets/all";
3536
import { cloneDeep, isEqual } from "lodash";
3637
import { ICustomFormatting, ICustomFormattingBodySection, ICustomFormattingNode } from "../../common/utilities/ICustomFormatting";
@@ -605,11 +606,12 @@ export class DynamicForm extends React.Component<
605606
/["|*|:|<|>|?|/|\\||]/g,
606607
"_"
607608
) // Replace not allowed chars in folder name
608-
: ""; // Empty string will be replaced by SPO with Folder Item ID
609-
const newFolder = await library.rootFolder.addSubFolderUsingPath(
610-
folderTitle
611-
);
609+
: ""; // Empty string will be replaced by SPO with Folder Item ID
610+
611+
const folder = !this.props.folderPath ? library.rootFolder : await this.getFolderByPath(this.props.folderPath, library.rootFolder);
612+
const newFolder = await folder.addSubFolderUsingPath( folderTitle );
612613
const fields = await newFolder.listItemAllFields();
614+
613615
if (fields[idField]) {
614616
// Read the ID of the just created folder or Document Set
615617
const folderId = fields[idField];
@@ -683,8 +685,9 @@ export class DynamicForm extends React.Component<
683685
"_"
684686
) // Replace not allowed chars in folder name
685687
: ""; // Empty string will be replaced by SPO with Folder Item ID
686-
687-
const fileCreatedResult = await library.rootFolder.files.addChunked(encodeURI(itemTitle), await selectedFile.downloadFileContent());
688+
689+
const folder = !this.props.folderPath ? library.rootFolder : await this.getFolderByPath(this.props.folderPath, library.rootFolder);
690+
const fileCreatedResult = await folder.files.addChunked(encodeURI(itemTitle), await selectedFile.downloadFileContent());
688691
const fields = await fileCreatedResult.file.listItemAllFields();
689692

690693
if (fields[idField]) {
@@ -1476,4 +1479,9 @@ export class DynamicForm extends React.Component<
14761479
}
14771480
}
14781481

1482+
private getFolderByPath = async (listRelativeFolderPath: string, rootFolder: IFolder): Promise<IFolder> => {
1483+
const libraryFolder = await rootFolder();
1484+
const folder = sp.web.getFolderByServerRelativePath(`${libraryFolder.ServerRelativeUrl}/${listRelativeFolderPath}`);
1485+
return folder;
1486+
};
14791487
}

src/controls/dynamicForm/IDynamicFormProps.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,11 @@ export interface IDynamicFormProps {
139139
* @default true
140140
*/
141141
storeLastActiveTab?: boolean;
142+
143+
/**
144+
* Library relative folder to create the item in.
145+
* 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.
146+
* Defaults to the root folder.
147+
*/
148+
folderPath?: string;
142149
}

0 commit comments

Comments
 (0)