Skip to content

Commit 0fb16d8

Browse files
Adds retry to updating list item in DynamicForm. Closes #1919
1 parent aff9a52 commit 0fb16d8

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

src/controls/dynamicForm/DynamicForm.tsx

+31-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import "@pnp/sp/content-types";
3232
import "@pnp/sp/folders";
3333
import "@pnp/sp/items";
3434
import { IFolder } from "@pnp/sp/folders";
35-
import { IInstalledLanguageInfo } from "@pnp/sp/presets/all";
35+
import { IInstalledLanguageInfo, IItemUpdateResult, IList } from "@pnp/sp/presets/all";
3636
import { cloneDeep, isEqual } from "lodash";
3737
import { ICustomFormatting, ICustomFormattingBodySection, ICustomFormattingNode } from "../../common/utilities/ICustomFormatting";
3838
import SPservice from "../../services/SPService";
@@ -49,6 +49,10 @@ import { Icon } from "@fluentui/react/lib/Icon";
4949

5050
const stackTokens: IStackTokens = { childrenGap: 20 };
5151

52+
const timeout = (ms: number): Promise<void> => {
53+
return new Promise((resolve) => setTimeout(resolve, ms));
54+
};
55+
5256
/**
5357
* DynamicForm Class Control
5458
*/
@@ -611,7 +615,7 @@ export class DynamicForm extends React.Component<
611615
// Set the content type ID for the target item
612616
objects[contentTypeIdField] = contentTypeId;
613617
// 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);
615619
if (onSubmitted) {
616620
onSubmitted(
617621
iur.data,
@@ -689,7 +693,7 @@ export class DynamicForm extends React.Component<
689693
// Set the content type ID for the target item
690694
objects[contentTypeIdField] = contentTypeId;
691695
// Update the just created file
692-
const iur = await library.items.getById(fileId).update(objects);
696+
const iur = await this.updateListItemRetry(library, fileId, objects);
693697
if (onSubmitted) {
694698
onSubmitted(
695699
iur.data,
@@ -1521,4 +1525,27 @@ export class DynamicForm extends React.Component<
15211525
const folder = sp.web.getFolderByServerRelativePath(`${serverRelativeLibraryPath}/${normalizedFolderPath}`);
15221526
return folder;
15231527
};
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

Comments
 (0)