Update item and move document using spfx extension
-
21-02-2021 - |
Pregunta
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();
});
}
Solución
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);
});
});
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:
Licenciado bajo: CC-BY-SA con atribución
No afiliado a sharepoint.stackexchange