【问题标题】:Update list item using SharePoint REST API not working使用 SharePoint REST API 更新列表项不起作用
【发布时间】:2023-04-06 02:20:01
【问题描述】:

我在使用 SharePoint REST API 更新简单列表项时遇到问题。我浏览了所有博客以获得解决方案,但结果是一样的。当我使用 REST API 执行更新列表项功能时,它会返回我尝试更新的特定列表项行的正文,但它不会更新列表项。有人可以帮我吗。提前致谢

    function (listTitle, TabId, success, failure) {
     var itemType = GetItemTypeForListName(listTitle);
     var query = appweburl + "_api/SP.AppContextSite(@target)/web/lists/getbytitle('" + listTitle + "')/items(5)?&@target='" + hostweburl + "'";

      var meta_data = {"__metadata": { "type": itemType }};
                    meta_data['DeletedStatus'] = 'Inactive'
      var executor = new SP.RequestExecutor(appweburl);

      executor.executeAsync({
                    url: query,
                    type: "POST",
                    contentType: "application/json;odata=verbose",
                    data: JSON.stringify(meta_data),
                    //body: meta_data,
                    headers: {
                        "Accept": "application/json;odata=verbose",
                        "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                        "X-HTTP-Method": "MERGE",
                        "IF-MATCH": "*"
                        //"content-length": meta_data.length,

                    },
                    success: function (data) {
                        //alert("success: " + JSON.stringify(data));
                        //deferred.resolve(JSON.parse(data.body));
                        console.log(JSON.stringify(data));
                        alert(JSON.stringify(data));
                    },
                    error: function (err) {
                        //alert("error: " + JSON.stringify(err));
                        console.log(JSON.stringify(err));
                    }
                });

}

function GetItemTypeForListName(name) {
                return "SP.Data." + name.charAt(0).toUpperCase() + name.split(" ").join("").slice(1) + "ListItem";
            }

【问题讨论】:

  • 能写出异常信息吗?
  • 你可以试试方法:“POST”而不是类型吗?
  • 问题解决了吗??我有类似的问题。

标签: rest sharepoint sharepoint-2013 sharepoint-apps


【解决方案1】:

否则只需替换函数中的以下代码

executor.executeAsync({
url : query,
method : "POST",
body: JSON.stringify(meta_data),
headers: {
    "Accept": "application/json;odata=verbose",
    "Content-Type" : "application/json;odata=verbose",
    "X-HTTP-Method": "MERGE",
    "IF-MATCH": "*"
},
success: function (data) {
    console.log(JSON.stringify(data));
},
error: function (err) {
    console.log(JSON.stringify(err));
} });

它还会为你插入摘要,所以你不需要添加

"X-RequestDigest": $("#__REQUESTDIGEST").val(),

【讨论】:

    猜你喜欢
    • 2019-06-06
    • 2014-10-26
    • 1970-01-01
    • 1970-01-01
    • 2021-03-05
    • 2021-05-19
    • 1970-01-01
    • 2019-07-13
    • 1970-01-01
    相关资源
    最近更新 更多