4

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";
            }
  • try constructing your post data like below var meta_data = { '__metadata': { 'type': itemType }, 'DeletedStatus': 'Inactive' }; – Azam Khan Jan 20 '16 at 14:21
  • Hi Azam, thanks for your response. I tried constructing the meta_data as suggested but the result is still the same. I still get the result of the item as json and when I look into the data.body the results that I got is pointing to the list item I want to update either DeletedStatus column value as old value(Not updated). Do you see any other issues in my code? – user545359 Jan 20 '16 at 15:08
3

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));
    }
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.