Skip to content

Commit d4bd766

Browse files
PaoloPiaAJIXuMuK
authored andcommitted
Initial fix for Document Sets and folders
1 parent bb59010 commit d4bd766

File tree

2 files changed

+48
-11
lines changed

2 files changed

+48
-11
lines changed

src/controls/dynamicForm/DynamicForm.tsx

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ import styles from './DynamicForm.module.scss';
1616
import { IDynamicFormProps } from './IDynamicFormProps';
1717
import { IDynamicFormState } from './IDynamicFormState';
1818

19+
import '@pnp/sp/lists';
20+
import '@pnp/sp/content-types';
21+
import '@pnp/sp/folders';
22+
import '@pnp/sp/items';
23+
1924
const stackTokens: IStackTokens = { childrenGap: 20 };
2025

2126
/**
@@ -103,6 +108,7 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
103108
const {
104109
listId,
105110
listItemId,
111+
contentTypeId,
106112
onSubmitted,
107113
onBeforeSubmit,
108114
onSubmitError
@@ -215,6 +221,7 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
215221
}
216222
}
217223

224+
// If we have the item ID, we simply need to update it
218225
if (listItemId) {
219226
try {
220227
const iur = await sp.web.lists.getById(listId).items.getById(listItemId).update(objects);
@@ -231,19 +238,48 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
231238

232239
}
233240
else {
234-
try {
235-
const iar = await sp.web.lists.getById(listId).items.add(objects);
236-
if (onSubmitted) {
237-
onSubmitted(iar.data, this.props.returnListItemInstanceOnSubmit !== false ? iar.item : undefined);
241+
242+
// Otherwise, depending on the content type ID of the item, if any, we need to behave accordingly
243+
if (!contentTypeId || !contentTypeId.startsWith('0x0120')) {
244+
// We are adding a new list item
245+
try {
246+
const iar = await sp.web.lists.getById(listId).items.add(objects);
247+
if (onSubmitted) {
248+
onSubmitted(iar.data, this.props.returnListItemInstanceOnSubmit !== false ? iar.item : undefined);
249+
}
250+
}
251+
catch (error) {
252+
if (onSubmitError) {
253+
onSubmitError(objects, error);
254+
}
255+
console.log("Error", error);
256+
}
257+
} else if (contentTypeId.startsWith('0x0120')) {
258+
// We are adding a folder or a Document Set
259+
try {
260+
const library = await sp.web.lists.getById(listId);
261+
const newFolder = await library.rootFolder.addSubFolderUsingPath(objects['Title']);
262+
const fields = await newFolder.listItemAllFields();
263+
const folderId = fields['ID'];
264+
265+
// Set the content type ID for the target item
266+
objects['ContentTypeId'] = contentTypeId;
267+
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);
272+
}
238273
}
239-
}
240-
catch (error) {
241-
if (onSubmitError) {
242-
onSubmitError(objects, error);
274+
catch (error) {
275+
if (onSubmitError) {
276+
onSubmitError(objects, error);
277+
}
278+
console.log("Error", error);
243279
}
244-
console.log("Error", error);
245280
}
246281
}
282+
247283
this.setState({
248284
isSaving: false
249285
});
@@ -313,10 +349,10 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
313349
const defaultContentType = await spList.contentTypes.select("Id", "Name").get();
314350
contentTypeId = defaultContentType[0].Id.StringValue;
315351
}
316-
const listFeilds = await this.getFormFields(listId, contentTypeId, this.webURL);
352+
const listFields = await this.getFormFields(listId, contentTypeId, this.webURL);
317353
const tempFields: IDynamicFieldProps[] = [];
318354
let order: number = 0;
319-
const responseValue = listFeilds.value;
355+
const responseValue = listFields.value;
320356
const hiddenFields = this.props.hiddenFields !== undefined ? this.props.hiddenFields : [];
321357
let defaultDayOfWeek: number = 0;
322358
for (let i = 0, len = responseValue.length; i < len; i++) {

src/controls/dynamicForm/dynamicField/DynamicField.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ export class DynamicField extends React.Component<IDynamicFieldProps, IDynamicFi
223223
defaultSelectedItems={defaultValue}
224224
columnInternalName={lookupField}
225225
className={styles.feildDisplay}
226+
enableDefaultSuggestions={true}
226227
keyColumnInternalName='Id'
227228
itemLimit={1}
228229
onSelectedItem={(newValue) => { this.onChange(newValue); }}

0 commit comments

Comments
 (0)