如果使用pnpjs更新一个item的某个或者某几个field的值,通常使用update方法,具体使用方式可以参见使用pnp添加或者更新一个field的值这篇博客。这里介绍的是另一种方法“validateUpdateListItem”,使用这个方法可以免去构造一些对象的麻烦,如果更新失败,还会返回具体的错误信息。
基本使用方法:
const list = sp.web.lists.getByTitle("list Title");
list.items.getById(1).validateUpdateListItem({FieldName: "Title", FieldValue: "MyTitle"}).then(r=>{console.log(r);}).catch(e=>{console.log(e);});
参数:{FieldName: string, FieldValue: string}
返回值:{"ErrorMessage": null, "FieldName": "Title", "FieldValue": "MyTitle", "HasException": false, "ItemId": 1}
其中参数里的FieldName是指field的InternalName.
如果正确更新field的值,返回值对象中的HasException为false,ErrorMessage为null,如果更新field失败,返回值对象中的HasExcept