Question

hi i am trying below code to update status of selected rows and after that move all selected documents to another library but document get moved without updating status please find below snippet

  event.selectedRows.forEach((row: RowAccessor, index: number) => {
            let list = sp.web.lists.getByTitle("Control Documents");
            const itemid=row.getValueByName("ID")
             const i =  list.items.getById(itemid).update({
              BMSTwoApprovalStatus:"Retired"
               }); 
             const docRelativeUrlDest :string = row.getValueByName('FileLeafRef');
             const destinationUrl = this.context.pageContext.web.serverRelativeUrl+'/RetiredDocuments/' + `${docRelativeUrlDest}`;
             const docRelativeUrl :string = row.getValueByName('FileRef');
             sp.web.getFileByUrl(docRelativeUrl).moveTo(destinationUrl);
           
             //this.updateListitem(itemid)
              this.showToastr();
          });

      }
Was it helpful?

Solution

PnP Js Update list item is async request, please use await or .then to make sure the update opeartion finished, then move file to another library, here is the modified code with .then function:

event.selectedRows.forEach((row: RowAccessor, index: number) => {
   let list = sp.web.lists.getByTitle("Control Documents");
   const itemid=row.getValueByName("ID")
   const i =  list.items.getById(itemid).update({
    BMSTwoApprovalStatus:"Retired"
     }).then((iar: IItemAddResult) => {
      console.log(iar);
      const docRelativeUrlDest :string = row.getValueByName('FileLeafRef');
      const destinationUrl = this.context.pageContext.web.serverRelativeUrl+'/RetiredDocuments/' + `${docRelativeUrlDest}`;
      const docRelativeUrl :string = row.getValueByName('FileRef');
      sp.web.getFileByUrl(docRelativeUrl).moveTo(destinationUrl);
  })
  .catch((error:any) => {
      console.log("Error: ", error);
  });
});

enter image description here

Also set "Require Check Out" in Versioning Settings to No in Control Documents, so that won't need to check out file firstly before updating metadata:

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top