@@ -32,7 +32,7 @@ import "@pnp/sp/content-types";
32
32
import "@pnp/sp/folders" ;
33
33
import "@pnp/sp/items" ;
34
34
import { IFolder } from "@pnp/sp/folders" ;
35
- import { IInstalledLanguageInfo } from "@pnp/sp/presets/all" ;
35
+ import { IInstalledLanguageInfo , IItemUpdateResult , IList } from "@pnp/sp/presets/all" ;
36
36
import { cloneDeep , isEqual } from "lodash" ;
37
37
import { ICustomFormatting , ICustomFormattingBodySection , ICustomFormattingNode } from "../../common/utilities/ICustomFormatting" ;
38
38
import SPservice from "../../services/SPService" ;
@@ -49,6 +49,10 @@ import { Icon } from "@fluentui/react/lib/Icon";
49
49
50
50
const stackTokens : IStackTokens = { childrenGap : 20 } ;
51
51
52
+ const timeout = ( ms : number ) : Promise < void > => {
53
+ return new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
54
+ } ;
55
+
52
56
/**
53
57
* DynamicForm Class Control
54
58
*/
@@ -611,7 +615,7 @@ export class DynamicForm extends React.Component<
611
615
// Set the content type ID for the target item
612
616
objects [ contentTypeIdField ] = contentTypeId ;
613
617
// Update the just created folder or Document Set
614
- const iur = await library . items . getById ( folderId ) . update ( objects ) ;
618
+ const iur = await this . updateListItemRetry ( library , folderId , objects ) ;
615
619
if ( onSubmitted ) {
616
620
onSubmitted (
617
621
iur . data ,
@@ -689,7 +693,7 @@ export class DynamicForm extends React.Component<
689
693
// Set the content type ID for the target item
690
694
objects [ contentTypeIdField ] = contentTypeId ;
691
695
// Update the just created file
692
- const iur = await library . items . getById ( fileId ) . update ( objects ) ;
696
+ const iur = await this . updateListItemRetry ( library , fileId , objects ) ;
693
697
if ( onSubmitted ) {
694
698
onSubmitted (
695
699
iur . data ,
@@ -1521,4 +1525,27 @@ export class DynamicForm extends React.Component<
1521
1525
const folder = sp . web . getFolderByServerRelativePath ( `${ serverRelativeLibraryPath } /${ normalizedFolderPath } ` ) ;
1522
1526
return folder ;
1523
1527
} ;
1524
- }
1528
+
1529
+ /**
1530
+ * Updates a list item and retries the operation if a 409 (Save Conflict) was thrown.
1531
+ * @param list The list/library on which to execute the operation
1532
+ * @param itemId The item ID
1533
+ * @param objects The values to update the item with
1534
+ * @param retry The retry index
1535
+ * @returns An update result
1536
+ */
1537
+ private updateListItemRetry = async ( list : IList , itemId : number , objects : { } , retry : number = 0 ) : Promise < IItemUpdateResult > => {
1538
+ try {
1539
+ return await list . items . getById ( itemId ) . update ( objects , "1" ) ;
1540
+ }
1541
+ catch ( error )
1542
+ {
1543
+ if ( error . status === 409 && retry < 3 ) {
1544
+ await timeout ( 100 ) ;
1545
+ return await this . updateListItemRetry ( list , itemId , objects , retry + 1 ) ;
1546
+ }
1547
+
1548
+ throw error ;
1549
+ }
1550
+ }
1551
+ }
0 commit comments