@@ -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 ) ;
@@ -230,20 +237,61 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
230
237
}
231
238
232
239
}
240
+ // Otherwise, depending on the content type ID of the item, if any, we need to behave accordingly
233
241
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 ) ;
242
+ if ( contentTypeId === undefined || contentTypeId === '' || ! contentTypeId . startsWith ( '0x0120' ) ) {
243
+ // We are adding a new list item
244
+ try {
245
+ const iar = await sp . web . lists . getById ( listId ) . items . add ( objects ) ;
246
+ if ( onSubmitted ) {
247
+ onSubmitted ( iar . data , this . props . returnListItemInstanceOnSubmit !== false ? iar . item : undefined ) ;
248
+ }
249
+ }
250
+ catch ( error ) {
251
+ if ( onSubmitError ) {
252
+ onSubmitError ( objects , error ) ;
253
+ }
254
+ console . log ( "Error" , error ) ;
255
+ }
256
+ } else if ( contentTypeId !== undefined && contentTypeId !== '' && contentTypeId . startsWith ( '0x0120' ) ) {
257
+ // We are adding a folder or a Document Set
258
+ try {
259
+ const idField = 'ID' ;
260
+ const titleField = 'Title' ;
261
+ const contentTypeIdField = 'ContentTypeId' ;
262
+
263
+ const library = await sp . web . lists . getById ( listId ) ;
264
+ const folderTitle = ( objects [ titleField ] !== undefined && objects [ titleField ] !== '' ) ?
265
+ ( objects [ titleField ] as string ) . replace ( / [ " | * | : | < | > | ? | / | \\ | | ] / g, "_" ) : // Replace not allowed chars in folder name
266
+ '' ; // Empty string will be replaced by SPO with Folder Item ID
267
+ const newFolder = await library . rootFolder . addSubFolderUsingPath ( folderTitle ) ;
268
+ const fields = await newFolder . listItemAllFields ( ) ;
269
+ if ( fields [ idField ] ) {
270
+
271
+ // Read the ID of the just created folder or Document Set
272
+ const folderId = fields [ idField ] ;
273
+
274
+ // Set the content type ID for the target item
275
+ objects [ contentTypeIdField ] = contentTypeId ;
276
+
277
+ // Update the just created folder or Document Set
278
+ const iur = await library . items . getById ( folderId ) . update ( objects ) ;
279
+ if ( onSubmitted ) {
280
+ onSubmitted ( iur . data , this . props . returnListItemInstanceOnSubmit !== false ? iur . item : undefined ) ;
281
+ }
282
+ } else {
283
+ throw new Error ( 'Unable to read the ID of the just created folder or Document Set' ) ;
284
+ }
238
285
}
239
- }
240
- catch ( error ) {
241
- if ( onSubmitError ) {
242
- onSubmitError ( objects , error ) ;
286
+ catch ( error ) {
287
+ if ( onSubmitError ) {
288
+ onSubmitError ( objects , error ) ;
289
+ }
290
+ console . log ( "Error" , error ) ;
243
291
}
244
- console . log ( "Error" , error ) ;
245
292
}
246
293
}
294
+
247
295
this . setState ( {
248
296
isSaving : false
249
297
} ) ;
@@ -313,10 +361,10 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
313
361
const defaultContentType = await spList . contentTypes . select ( "Id" , "Name" ) . get ( ) ;
314
362
contentTypeId = defaultContentType [ 0 ] . Id . StringValue ;
315
363
}
316
- const listFeilds = await this . getFormFields ( listId , contentTypeId , this . webURL ) ;
364
+ const listFields = await this . getFormFields ( listId , contentTypeId , this . webURL ) ;
317
365
const tempFields : IDynamicFieldProps [ ] = [ ] ;
318
366
let order : number = 0 ;
319
- const responseValue = listFeilds . value ;
367
+ const responseValue = listFields . value ;
320
368
const hiddenFields = this . props . hiddenFields !== undefined ? this . props . hiddenFields : [ ] ;
321
369
let defaultDayOfWeek : number = 0 ;
322
370
for ( let i = 0 , len = responseValue . length ; i < len ; i ++ ) {
@@ -537,7 +585,11 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
537
585
const webAbsoluteUrl = ! webUrl ? this . webURL : webUrl ;
538
586
let apiUrl = '' ;
539
587
if ( contentTypeId !== undefined && contentTypeId !== '' ) {
540
- apiUrl = `${ webAbsoluteUrl } /_api/web/lists(@listId)/contenttypes('${ contentTypeId } ')/fields?@listId=guid'${ encodeURIComponent ( listId ) } '&$filter=ReadOnlyField eq false and Hidden eq false and (FromBaseType eq false or StaticName eq 'Title')` ;
588
+ if ( contentTypeId . startsWith ( '0x0120' ) ) {
589
+ apiUrl = `${ webAbsoluteUrl } /_api/web/lists(@listId)/contenttypes('${ contentTypeId } ')/fields?@listId=guid'${ encodeURIComponent ( listId ) } '&$filter=ReadOnlyField eq false and (Hidden eq false or StaticName eq 'Title') and (FromBaseType eq false or StaticName eq 'Title')` ;
590
+ } else {
591
+ apiUrl = `${ webAbsoluteUrl } /_api/web/lists(@listId)/contenttypes('${ contentTypeId } ')/fields?@listId=guid'${ encodeURIComponent ( listId ) } '&$filter=ReadOnlyField eq false and Hidden eq false and (FromBaseType eq false or StaticName eq 'Title')` ;
592
+ }
541
593
}
542
594
else {
543
595
apiUrl = `${ webAbsoluteUrl } /_api/web/lists(@listId)/fields?@listId=guid'${ encodeURIComponent ( listId ) } '&$filter=ReadOnlyField eq false and Hidden eq false and (FromBaseType eq false or StaticName eq 'Title')` ;
0 commit comments