@@ -16,6 +16,11 @@ import styles from './DynamicForm.module.scss';
16
16
import { IDynamicFormProps } from './IDynamicFormProps' ;
17
17
import { IDynamicFormState } from './IDynamicFormState' ;
18
18
19
+ import '@pnp/sp/lists' ;
20
+ import '@pnp/sp/content-types' ;
21
+ import '@pnp/sp/folders' ;
22
+ import '@pnp/sp/items' ;
23
+
19
24
const stackTokens : IStackTokens = { childrenGap : 20 } ;
20
25
21
26
/**
@@ -103,6 +108,7 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
103
108
const {
104
109
listId,
105
110
listItemId,
111
+ contentTypeId,
106
112
onSubmitted,
107
113
onBeforeSubmit,
108
114
onSubmitError
@@ -215,6 +221,7 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
215
221
}
216
222
}
217
223
224
+ // If we have the item ID, we simply need to update it
218
225
if ( listItemId ) {
219
226
try {
220
227
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
231
238
232
239
}
233
240
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
+ }
238
273
}
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 ) ;
243
279
}
244
- console . log ( "Error" , error ) ;
245
280
}
246
281
}
282
+
247
283
this . setState ( {
248
284
isSaving : false
249
285
} ) ;
@@ -313,10 +349,10 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
313
349
const defaultContentType = await spList . contentTypes . select ( "Id" , "Name" ) . get ( ) ;
314
350
contentTypeId = defaultContentType [ 0 ] . Id . StringValue ;
315
351
}
316
- const listFeilds = await this . getFormFields ( listId , contentTypeId , this . webURL ) ;
352
+ const listFields = await this . getFormFields ( listId , contentTypeId , this . webURL ) ;
317
353
const tempFields : IDynamicFieldProps [ ] = [ ] ;
318
354
let order : number = 0 ;
319
- const responseValue = listFeilds . value ;
355
+ const responseValue = listFields . value ;
320
356
const hiddenFields = this . props . hiddenFields !== undefined ? this . props . hiddenFields : [ ] ;
321
357
let defaultDayOfWeek : number = 0 ;
322
358
for ( let i = 0 , len = responseValue . length ; i < len ; i ++ ) {
0 commit comments