Updating SharePoint Created, Modified, Created By, Modified By field of list item is not straight forward and it can’t be done through simple list item update. To achieve this, we can make use of validateUpdateListItem from PnP js and make sure the user or the service account which runs the script has Full control of the list.
In the below function, we are updating Modified By, Modified (to yesterday date) and Created By value of the SharePoint list item, and it gets single param which is of type Item (@pnp/sp/items)
const updateSPItemSystemVal = async (item: Item) => { const userEmailToFormString = (userName: string): string => { return JSON.stringify([{ Key: userName }]); }; const sysUpdateData = [ { FieldName: 'Editor', FieldValue: userEmailToFormString("<emailaddress>") }, { FieldName: 'Author', FieldValue: userEmailToFormString("<emailaddress>") } ]; const result = await item.validateUpdateListItem(sysUpdateData); const errors = result.filter(field => field.ErrorMessage !== null); if (errors.length > 0) { throw new Error(JSON.stringify(errors)); } return result; };
For more info please refer, pnp js doc
Happy Coding
Fazil
Leave a comment