@@ -54,7 +54,7 @@ const stackTokens: IStackTokens = { childrenGap: 20 };
54
54
export class DynamicForm extends React . Component <
55
55
IDynamicFormProps ,
56
56
IDynamicFormState
57
- > {
57
+ > {
58
58
private _spService : SPservice ;
59
59
private _formulaEvaluation : FormulaEvaluation ;
60
60
private _customFormatter : CustomFormattingHelper ;
@@ -594,22 +594,14 @@ export class DynamicForm extends React.Component<
594
594
else if ( contentTypeId . startsWith ( "0x0120" ) ) {
595
595
// We are adding a folder or a Document Set
596
596
try {
597
- const idField = "ID" ;
598
- const titleField = "Title" ;
597
+ const idField = "ID" ;
599
598
const contentTypeIdField = "ContentTypeId" ;
600
599
601
- const library = await sp . web . lists . getById ( listId ) ;
602
- const folderTitle =
603
- objects [ titleField ] !== undefined && objects [ titleField ] !== ""
604
- ? ( objects [ titleField ] as string ) . replace (
605
- / [ " | * | : | < | > | ? | / | \\ | | ] / g,
606
- "_"
607
- ) // Replace not allowed chars in folder name
608
- : "" ; // Empty string will be replaced by SPO with Folder Item ID
609
- const newFolder = await library . rootFolder . addSubFolderUsingPath (
610
- folderTitle
611
- ) ;
600
+ const library = await sp . web . lists . getById ( listId ) ;
601
+ const folderFileName = this . getFolderName ( objects ) ;
602
+ const newFolder = await library . rootFolder . addSubFolderUsingPath ( folderFileName ) ;
612
603
const fields = await newFolder . listItemAllFields ( ) ;
604
+
613
605
if ( fields [ idField ] ) {
614
606
// Read the ID of the just created folder or Document Set
615
607
const folderId = fields [ idField ] ;
@@ -681,20 +673,20 @@ export class DynamicForm extends React.Component<
681
673
? ( selectedFile . fileName as string ) . replace (
682
674
/ [ " | * | : | < | > | ? | / | \\ | | ] / g,
683
675
"_"
684
- ) // Replace not allowed chars in folder name
676
+ ) . trim ( ) // Replace not allowed chars in folder name and trim empty spaces at the start or end.
685
677
: "" ; // Empty string will be replaced by SPO with Folder Item ID
686
678
687
679
const fileCreatedResult = await library . rootFolder . files . addChunked ( encodeURI ( itemTitle ) , await selectedFile . downloadFileContent ( ) ) ;
688
680
const fields = await fileCreatedResult . file . listItemAllFields ( ) ;
689
681
690
682
if ( fields [ idField ] ) {
691
- // Read the ID of the just created folder or Document Set
692
- const folderId = fields [ idField ] ;
683
+ // Read the ID of the just created file
684
+ const fileId = fields [ idField ] ;
693
685
694
686
// Set the content type ID for the target item
695
687
objects [ contentTypeIdField ] = contentTypeId ;
696
- // Update the just created folder or Document Set
697
- const iur = await library . items . getById ( folderId ) . update ( objects ) ;
688
+ // Update the just created file
689
+ const iur = await library . items . getById ( fileId ) . update ( objects ) ;
698
690
if ( onSubmitted ) {
699
691
onSubmitted (
700
692
iur . data ,
@@ -705,7 +697,7 @@ export class DynamicForm extends React.Component<
705
697
}
706
698
} else {
707
699
throw new Error (
708
- "Unable to read the ID of the just created folder or Document Set "
700
+ "Unable to read the ID of the just created file "
709
701
) ;
710
702
}
711
703
} catch ( error ) {
@@ -990,9 +982,17 @@ export class DynamicForm extends React.Component<
990
982
// Load SharePoint list item
991
983
const spList = sp . web . lists . getById ( listId ) ;
992
984
let item = null ;
985
+ const isEditingItem = listItemId !== undefined && listItemId !== null && listItemId !== 0 ;
993
986
let etag : string | undefined = undefined ;
994
- if ( listItemId !== undefined && listItemId !== null && listItemId !== 0 ) {
995
- item = await spList . items . getById ( listItemId ) . get ( ) . catch ( err => this . updateFormMessages ( MessageBarType . error , err . message ) ) ;
987
+
988
+ if ( isEditingItem ) {
989
+ const spListItem = spList . items . getById ( listItemId ) ;
990
+
991
+ if ( contentTypeId . startsWith ( "0x0120" ) || contentTypeId . startsWith ( "0x0101" ) ) {
992
+ spListItem . select ( "*" , "FileLeafRef" ) ; // Explainer: FileLeafRef is not loaded by default. Load it to show the file/folder name in the field.
993
+ }
994
+
995
+ item = await spListItem . get ( ) . catch ( err => this . updateFormMessages ( MessageBarType . error , err . message ) ) ;
996
996
997
997
if ( onListItemLoaded ) {
998
998
await onListItemLoaded ( item ) ;
@@ -1288,7 +1288,7 @@ export class DynamicForm extends React.Component<
1288
1288
if ( defaultValue !== undefined && defaultValue !== null ) defaultValue = Boolean ( Number ( defaultValue ) ) ;
1289
1289
if ( value !== undefined && value !== null ) value = Boolean ( Number ( value ) ) ;
1290
1290
}
1291
-
1291
+
1292
1292
tempFields . push ( {
1293
1293
value,
1294
1294
newValue : undefined ,
@@ -1476,4 +1476,24 @@ export class DynamicForm extends React.Component<
1476
1476
}
1477
1477
}
1478
1478
1479
+ /**
1480
+ * Creates a folder name based on the FileLeafRef field (if rendered) or the Title field (if rendered)
1481
+ * Replaces not allowed chars in folder name and trims spaces at the start and end of the string
1482
+ * Empty string will be replaced by SPO with Folder Item ID
1483
+ * @param objects The object containing the field values
1484
+ * @returns the folder name
1485
+ */
1486
+ private getFolderName = ( objects : { } ) : string => {
1487
+ const titleField = "Title" ;
1488
+ const fileLeafRefField = "FileLeafRef" ;
1489
+ let folderNameValue = "" ;
1490
+
1491
+ if ( objects [ fileLeafRefField ] !== undefined && objects [ fileLeafRefField ] !== "" )
1492
+ folderNameValue = objects [ fileLeafRefField ] as string ;
1493
+
1494
+ if ( objects [ titleField ] !== undefined && objects [ titleField ] !== "" )
1495
+ folderNameValue = objects [ titleField ] as string ;
1496
+
1497
+ return folderNameValue . replace ( / [ " | * | : | < | > | ? | / | \\ | | ] / g, "_" ) . trim ( ) ;
1498
+ }
1479
1499
}
0 commit comments