Recently, I was trying to create a list item using 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 is invalid and might be corrupted. Please                            use 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 has been expired on the page. In that case you 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);

最新文章

  1. 跨应用使用Spoon框架截图的方法
  2. 项目部署到tomcat Root中后导致 WebApplicationContext 初始化两次的解决方法
  3. MAC机中安装ruby环境--转载
  4. 移动端WEB开发,click,touch,tap事件浅析
  5. asp.net 网站 或者web Api 发布
  6. Struts2_ValueStack,OGNL详解
  7. Java遇见HTML——JSP篇之JSP内置对象(下)
  8. QC、IQC、IPQC、FQC、OQC
  9. 编译安装vim8.0
  10. Set Linux starts in multi-user mode as default.
  11. C 宏定义
  12. Protobuf完整实例
  13. Android Bug 记录
  14. Java中@Deprecated、@SupressWarning、@Override的作用
  15. c++ 如何获取系统时间 - zjnig711的信息仓库 - 博客频道 - CSDN.NET
  16. es6基础系列二:Number
  17. codeforces 258D
  18. Linux入门(4)——Ubuntu16.04安装MATLAB2016b
  19. spring容器和springmvc容器,以及web容器的关系
  20. 转 git push 提示 Everything up-to-date

热门文章

  1. ES6学习笔记(二)-字符串的扩展
  2. NodeList、HTMLCollection和NamedNodeMap
  3. MyEclipse去除网上复制下来的代码带有的行号(使用正则表达式)
  4. react生命周期es6
  5. 微信小程序开发11-HTTPS网络通信(重点)
  6. 利用Vagrant完成开发环境配置
  7. dbcp2、c3p0、druid连接池的简单配置
  8. Gartner发布最新魔力象限报告,微软领跑数据库市场(编译自TechRepublic)
  9. js变量作用域--变量提升
  10. SVN合并时报错:合并跟踪不允许丢失子树Merge tracking not allowed with missing subtrees; try restoring these items