Skip to content

Commit

Permalink
Adds ability to create files/folders in subfolder using DynamicForm. C…
Browse files Browse the repository at this point in the history
…loses pnp#1901
  • Loading branch information
martinlingstuyl committed Nov 13, 2024
1 parent 2bd431c commit 5eb14c9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
1 change: 1 addition & 0 deletions docs/documentation/docs/controls/DynamicForm.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +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. |

## Validation Error Dialog Properties `IValidationErrorDialogProps`
| Property | Type | Required | Description |
Expand Down
20 changes: 14 additions & 6 deletions src/controls/dynamicForm/DynamicForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import "@pnp/sp/lists";
import "@pnp/sp/content-types";
import "@pnp/sp/folders";
import "@pnp/sp/items";
import { IFolder } from "@pnp/sp/folders";
import { IInstalledLanguageInfo } from "@pnp/sp/presets/all";
import { cloneDeep, isEqual } from "lodash";
import { ICustomFormatting, ICustomFormattingBodySection, ICustomFormattingNode } from "../../common/utilities/ICustomFormatting";
Expand Down Expand Up @@ -605,11 +606,12 @@ export class DynamicForm extends React.Component<
/["|*|:|<|>|?|/|\\||]/g,
"_"
) // Replace not allowed chars in folder name
: ""; // Empty string will be replaced by SPO with Folder Item ID
const newFolder = await library.rootFolder.addSubFolderUsingPath(
folderTitle
);
: ""; // Empty string will be replaced by SPO with Folder Item ID

const folder = !this.props.folderPath ? library.rootFolder : await this.getFolderByPath(this.props.folderPath, library.rootFolder);
const newFolder = await folder.addSubFolderUsingPath( folderTitle );
const fields = await newFolder.listItemAllFields();

if (fields[idField]) {
// Read the ID of the just created folder or Document Set
const folderId = fields[idField];
Expand Down Expand Up @@ -683,8 +685,9 @@ export class DynamicForm extends React.Component<
"_"
) // Replace not allowed chars in folder name
: ""; // Empty string will be replaced by SPO with Folder Item ID

const fileCreatedResult = await library.rootFolder.files.addChunked(encodeURI(itemTitle), await selectedFile.downloadFileContent());

const folder = !this.props.folderPath ? library.rootFolder : await this.getFolderByPath(this.props.folderPath, library.rootFolder);
const fileCreatedResult = await folder.files.addChunked(encodeURI(itemTitle), await selectedFile.downloadFileContent());
const fields = await fileCreatedResult.file.listItemAllFields();

if (fields[idField]) {
Expand Down Expand Up @@ -1476,4 +1479,9 @@ export class DynamicForm extends React.Component<
}
}

private getFolderByPath = async (listRelativeFolderPath: string, rootFolder: IFolder): Promise<IFolder> => {
const libraryFolder = await rootFolder();
const folder = sp.web.getFolderByServerRelativePath(`${libraryFolder.ServerRelativeUrl}/${listRelativeFolderPath}`);
return folder;
};
}
7 changes: 7 additions & 0 deletions src/controls/dynamicForm/IDynamicFormProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,11 @@ export interface IDynamicFormProps {
* @default true
*/
storeLastActiveTab?: boolean;

/**
* 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;
}

0 comments on commit 5eb14c9

Please sign in to comment.