4

I am trying to make update with the usage of REST but I got an error:

-2130575251, Microsoft.SharePoint.SPException. The security validation for this page is invalid and might be corrupted. Please use your web browser's Back button to try your operation again.

I've read link here, that the reason could be that the digest has been expired on the page and I've implemented the helper method retrieving digest but without success.

Any ideas how to workaround the error? Here are my functions which do the update:

$(function() { 
    $('#send_button').click(function() { 
        var webUrl = "http://mycompanyserver.com/";
        return getFormDigest(webUrl).then(function (data) {
            var data = {
                __metadata: { 'type': 'SP.Data.ProjectsListItem' },
                Title: 'Please provide title here',
                RemainingWork: '8',
                Start_x0020_Date: new Date().toISOString()
            };
            $.ajax({ 
               url: webUrl + "/_api/web/lists/getbytitle('Tasks')/items(1)",
               method: "PATCH",
               data: JSON.stringify(data),
               headers: { "Accept": "application/json; odata=verbose",
                          "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                          "If-Match": "*"                                 
               },
               success: function (data) {
                   alert('Item added successfully');
               },
               error: function (error) {
                   alert("Error: "+ JSON.stringify(error));
               }
           });
      });
   });    
});

function getFormDigest(webUrl) {
    return $.ajax({
        url: webUrl + "/_api/contextinfo",
        method: "POST",
        headers: { "Accept": "application/json; odata=verbose" }
    });
}
  • Change method from "PATCH" to "POST" and try. if at all it didn't work then add "X-HTTP-Method":"MERGE" in header. – Azam Khan Dec 9 '15 at 10:23
  • Still nothing. I got: 'Error:{"readyState":0, "responseText":"", "status":0, "statusText":error }' from the function. – user3311556 Dec 9 '15 at 10:39
  • what i can sense is you are trying to update an item with ID "1" in the list called "Task". But ListItemEntityTypeFullName(__metadata: { 'type': 'SP.Data.ProjectsListItem' }) which you are passing doesn't match with the ListName. Are you passing the correct ListItemEntityTypeFullName ? – Azam Khan Dec 9 '15 at 13:21
2

The original error indeed occurs when digest has been expired or it could not be retrieved ( e.g. SharePoint:FormDigest control is not available on the page) on the page.

There are some another issues with the provided example. For updating list item the following parameters need to be specified:

method: "POST"

headers:
       "Accept": "application/json;odata=verbose",
       "X-Http-Method": "MERGE",
       "X-RequestDigest": form digest value
       "If-Match": etag or "*"

Also make sure the proper field names are specified (e.g. Start_x0020_Date). Assuming this is a Tasks based list that contains Start Date field, then you need to specify field internal name StartDate

And finally, make sure the valid entity type name for __metadata.type is specified. You could verify it via following query:

/_api/web/lists/getbytitle('<list title>')?$select=ListItemEntityTypeFullName

Example

function updateListItem(webUrl,listTitle,itemId,properties){
      var requestUrl = webUrl + "/_api/web/lists/getbytitle('" + listTitle +  "')/items(" + itemId + ")";
      var headers = {};
      headers["X-HTTP-Method"] = "MERGE";
      headers["If-Match"] =  "*";
      headers["X-RequestDigest"] = $("#__REQUESTDIGEST").val();
      return executeJson(requestUrl,"POST",headers,properties);
}



var webUrl = _spPageContextInfo.webAbsoluteUrl;
var listTitle = "Projects";
var itemId = 1;
var itemPayload = {
     __metadata: { 'type': 'SP.Data.ProjectsListItem' },
     Title: 'Prj #123',
     RemainingWork: '8',
     StartDate: new Date().toISOString()
};
updateListItem(webUrl,listTitle,itemId,itemPayload)
.done(function(item){
   console.log('Updated');
})
.fail(function(error){
    console.log(error.responseJSON.error.message.value);
});

where

executeJson.js is a wrapper for $.ajax function.

  • The error persisted even after above code. Eventually identified that it was to do with permissions. The user through which i was trying to add item was having read permissions. After setting permissions to contribute it worked. In summary - a very misleading error message. resulting into waste of time :( – shoab Sep 20 '18 at 7:06

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.