zoukankan      html  css  js  c++  java
  • sharepoint rest api Add ListItem 报错

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

  • 相关阅读:
    集合类说明及区别
    Liferay
    ASP项目部署IIS7.5中遇到的问题
    IBM X3650 M3/M4的服务器装系统
    在table中tr的display:block在firefox下显示布局错乱问题
    Firefox table 不居中解决办法 解决火狐层或 table 不居中
    Java进阶: File类的特点?如何创建File类对象?Java中如何操作文件内容,什么是Io流Io流如何读取和写入文件?字节缓冲流使用原则?
    计算机期末考试高频考试题目,附带题目解析
    java基础使用的集合大总结
    TreeMap集合根据指定元素,进行删除和修改bug梳理
  • 原文地址:https://www.cnblogs.com/olay/p/7867525.html
Copyright © 2011-2022 走看看