Skip to content

Commit 6248271

Browse files
PaoloPiaAJIXuMuK
authored andcommitted
Improved code quality for DynamicForm support of Document Sets
1 parent e78443c commit 6248271

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

src/controls/dynamicForm/DynamicForm.tsx

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,8 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
237237
}
238238

239239
}
240+
// Otherwise, depending on the content type ID of the item, if any, we need to behave accordingly
240241
else {
241-
242-
// Otherwise, depending on the content type ID of the item, if any, we need to behave accordingly
243242
if (contentTypeId === undefined || contentTypeId === '' || !contentTypeId.startsWith('0x0120')) {
244243
// We are adding a new list item
245244
try {
@@ -257,18 +256,30 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
257256
} else if (contentTypeId !== undefined && contentTypeId !== '' && contentTypeId.startsWith('0x0120')) {
258257
// We are adding a folder or a Document Set
259258
try {
259+
const idField = 'ID';
260+
const titleField = 'Title';
261+
const contentTypeIdField = 'ContentTypeId';
262+
260263
const library = await sp.web.lists.getById(listId);
261-
const newFolder = await library.rootFolder.addSubFolderUsingPath(objects['Title']);
264+
const folderTitle = (objects[titleField] !== undefined && objects[titleField] !== '') ?
265+
encodeURIComponent(objects[titleField]) : ''; // Empty string will be replaced by SPO with Folder Item ID
266+
const newFolder = await library.rootFolder.addSubFolderUsingPath(folderTitle);
262267
const fields = await newFolder.listItemAllFields();
263-
const folderId = fields['ID'];
268+
if (fields[idField] !== undefined) {
264269

265-
// Set the content type ID for the target item
266-
objects['ContentTypeId'] = contentTypeId;
270+
// Read the ID of the just created folder or Document Set
271+
const folderId = fields[idField];
267272

268-
// Update the just created folder or Document Set
269-
const iur = await library.items.getById(folderId).update(objects);
270-
if (onSubmitted) {
271-
onSubmitted(iur.data, this.props.returnListItemInstanceOnSubmit !== false ? iur.item : undefined);
273+
// Set the content type ID for the target item
274+
objects[contentTypeIdField] = contentTypeId;
275+
276+
// Update the just created folder or Document Set
277+
const iur = await library.items.getById(folderId).update(objects);
278+
if (onSubmitted) {
279+
onSubmitted(iur.data, this.props.returnListItemInstanceOnSubmit !== false ? iur.item : undefined);
280+
}
281+
} else {
282+
throw new Error('Unable to read the ID of the just created folder or Document Set');
272283
}
273284
}
274285
catch (error) {

0 commit comments

Comments
 (0)