Вопрос

I am facing issues with updating a simple list item using SharePoint REST API. I have gone through all the blogs to get a solution but the result is same. When ever i execute the update list item function using REST API it returns me the body of the particular list item row i am trying to update but it is not updating the listem. Could someone please help me out. Thanks in advance

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";
            }
Это было полезно?

Решение

I believe RequestExecutor uses method in place of type and body in place of data. It will also insert the digest for you, so you do not need to specify that. I don't recall if it accepts contentType; I usually stick it in with the headers. Try the following:

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));
    }
});
Лицензировано под: CC-BY-SA с атрибуция
Не связан с sharepoint.stackexchange
scroll top