Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamicformfoldersupport #1878

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/documentation/docs/controls/DynamicForm.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ The `DynamicForm` can be configured with the following properties:
| saveDisabled | boolean | no | Specifies if save button is disabled. |
| 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. |
| folderServerRelativeUrl | string | no | Server relative URL of the folder a new item or file gets created in. If not set a new item or file will be created in the root folder of the list or library.

## Validation Error Dialog Properties `IValidationErrorDialogProps`
| Property | Type | Required | Description |
Expand Down
18 changes: 14 additions & 4 deletions src/controls/dynamicForm/DynamicForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,13 @@ export class DynamicForm extends React.Component<
// check if item contenttype is passed, then update the object with content type id, else, pass the object
if (contentTypeId !== undefined && contentTypeId.startsWith("0x01")) objects[contentTypeIdField] = contentTypeId;
const iar = await sp.web.lists.getById(listId).items.add(objects);
if (this.props.folderServerRelativeUrl && this.props.folderServerRelativeUrl !== '' && iar.item) {
// Move list item to target folder. Only way to directly create the item in the target folder would be to use AddValidateUpdateItemUsingPath which uses a different structure for the item's data.
const values = await iar.item.fieldValuesAsText();
const fileRef = values['FileRef'];
const fileLeafRef = values['FileLeafRef'];
await sp.web.getFileByServerRelativePath(fileRef).moveTo(this.props.folderServerRelativeUrl + '/' + fileLeafRef);
}
if (onSubmitted) {
onSubmitted(
iar.data,
Expand Down Expand Up @@ -606,9 +613,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 newFolder = await library.rootFolder.addSubFolderUsingPath(
folderTitle
);
const newFolder = this.props.folderServerRelativeUrl && this.props.folderServerRelativeUrl !== '' ?
await sp.web.getFolderByServerRelativePath(this.props.folderServerRelativeUrl).addSubFolderUsingPath(folderTitle) :
await library.rootFolder.addSubFolderUsingPath(folderTitle);
const fields = await newFolder.listItemAllFields();
if (fields[idField]) {
// Read the ID of the just created folder or Document Set
Expand Down Expand Up @@ -684,7 +691,10 @@ 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 fileCreatedResult = this.props.folderServerRelativeUrl && this.props.folderServerRelativeUrl !== '' ?
await sp.web.getFolderByServerRelativePath(this.props.folderServerRelativeUrl).files.addChunked(encodeURI(itemTitle), await selectedFile.downloadFileContent())
:
await library.rootFolder.files.addChunked(encodeURI(itemTitle), await selectedFile.downloadFileContent());
const fields = await fileCreatedResult.file.listItemAllFields();

if (fields[idField]) {
Expand Down
7 changes: 6 additions & 1 deletion src/controls/dynamicForm/IDynamicFormProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,15 @@ export interface IDynamicFormProps {
*/
customIcons?: { [columnInternalName: string]: string };

/**
* Server relative URL of the folder a new item or file gets created in.
* If not set a new item or file will be created in the root folder of the list or library.
*/
folderServerRelativeUrl?: string;

/**
* Specify fields custom sorting.
* The value is the field internal name.
*/
fieldOrder?: string[]

}