How to update SharePoint list item system value using PnP JS

Ahamed Fazil Buhari
 
Senior Developer
April 21, 2020
 
Rate this article
 
Views
4286

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

Category : JavaScript, PNP, SharePoint

Author Info

Ahamed Fazil Buhari
 
Senior Developer
 
Rate this article
 
Ahamed is a Senior Developer and he has very good experience in the field of Microsoft Technologies, especially SharePoint, Azure, M365, SPFx, .NET and client side scripting - JavaScript, TypeScript, ...read more
 

Leave a comment