rest - 如何使用 sharepoint rest api 使用相同的方法更新和创建项目
问题描述
我有一个要求,如果列表项已经存在,我需要更新它,如果该项目不存在,我需要创建一个新的。当我从自定义表单获取数据以更新项目时,我需要从单一方法管理的一切。有没有办法在sharepoint online rest api中做到这一点?我正在使用以下方法来更新项目
public static UpdateSaveSectionC(formData: any,id:any): PromiseLike<string> {
// Return a promise
const siteURL= "https://abc.sharepoint.com/sites/process";
return new Promise((resolve, reject) => {
for (var i = 0; i < formData.Category.length; i++) {
const restUrl = siteURL + `/_api/web/lists/getbytitle('List')/items(${id[i]})`;
const headers = { 'accept': 'application/json;odata=verbose', 'content-Type': 'application/json;odata=verbose','X-HTTP-Method': 'MERGE','IF-MATCH': '*'};
const listTitle = "List";
const data = {
'__metadata': { 'type': 'SP.Data.' + listTitle + 'ListItem','results':[] },
Category: formData.Category[i],
Recommendationsuggestion: formData.Recommendationsuggestion[i],
}
Helper.executeJson(restUrl, "POST", headers, JSON.stringify($.extend(true,{}, data)))
.then((response) => {
// Resolve the request
resolve("success");
}).catch( (e) => {
if(e.responseJSON.error.message.value.indexOf("The request ETag value") != -1)
{
resolve("Please refresh the page, and resubmit your changes");
}
});
}
`
解决方案
使用rest api过滤列表以根据唯一键确认项目不存在,
/_api/web/lists/getbytitle('list title')/items?$filter=UniqueField eq 'value'
根据退回的物品数量来了解结果。
推荐阅读
- python - 使用正则表达式匹配最后一次出现
- azure - Azure Blob 错误:指定的 Blob 不存在,但 Blob 存在
- reactjs - ReactJS Firebase 检索批量上传图像的 URL
- python-3.x - fetchall() 的结果是否有一些 next() 可迭代
- kubernetes - 使用客户拥有的密钥加密 kubernetes pvc 存储
- jquery-select2 - 如果我们动态附加新选项,选择 2 不起作用
- typo3 - TYPO3 bootstrap_grids - 如何修改内容元素的标题?
- reactjs - 使用 react-hooks-testing-library 测试自定义钩子
- java - 为什么它说它已经使用构造函数定义了?
- javascript - 单击按钮时从选择控件中隐藏选项