Recently, I was trying to create a list item using the Rest API on Sharepoint 2013. I got the following error message
"Error": {
"Code": " -2130575251, Microsoft.SharePoint.SPException",
"Message": {
"Lang": "en-US",
"Value": "The Security validation for this page was invalid and might be corrupted. Please use the your Web browser ' s Back button to try your operation again. "
}
}
Solution:
I found the solution for the above issue, most likely this error occurs since form Digest have been expired on the page. In this case, could acquire a new form digest value by making a POST request to/_api/contextinfo endpoint. I ' ve implemented the helper method for retrieving form digest.
Example:
function Getformdigest (siteurl) {
Return $.ajax ({
Url:siteurl + "/_api/contextinfo",
Method: "POST",
Headers: {"Accept": "Application/json; Odata=verbose "}
});
}
function Createlistitem (SiteURL, ListName, ItemProperties) {
var itemType = Getitemtypeforlistname (listname);
itemproperties["__metadata"] = {"type": ItemType};
Return Getformdigest (SiteURL). Then (function (data) {
Return $.ajax ({
Url:siteurl + "/_api/web/lists/getbytitle ('" + ListName + "')/items",
Type: "POST",
Processdata:false,
ContentType: "Application/json;odata=verbose",
Data:JSON.stringify (ItemProperties),
Headers: {
"Accept": "Application/json;odata=verbose",
"X-requestdigest": Data.d.getcontextwebinformation.formdigestvalue
}
});
});
}
Fetch Metadata for list item
function Getitemtypeforlistname (name) {
Return "SP. Data. "+ Name.charat (0). toUpperCase () + Name.split (" "). Join (" "). Slice (1) +" ListItem ";
}
Create a list item
var itemproperties = {' title ': ' Title 1 '};
Createlistitem (_sppagecontextinfo.webabsoluteurl, ' Demo List ', itemproperties)
. Done (function (data) {
Console.log (' Item has been added successfully ');
})
. Fail (function (error) {
Console.log (json.stringify (Error));
});
Usage:
var itemproperties = {' title ': ' Title 1 '};
Createlistitem (_sppagecontextinfo.webabsoluteurl, ' Test List ', itemproperties);
SharePoint Rest API Add ListItem error