5

I need to do insert operation to SharePoint list. Below is my code..

$.ajax({
        url: appWebUrl + "/_api/SP.AppContextSite(@target)" + "/web/lists/getbytitle('NewList')/items?" + "@target='" + hostWebUrl + "'",
        type: "POST",
        data:  JSON.stringify({ '__metadata': { 'type': 'SP.Data.NewListListItem' },
        'Title': "TitleVal", 'Name':"Satya"}),       
        headers: { 
            "accept": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),
            "content-Type": "application/json;odata=verbose" },
        success: function (data) {
            success(data);
        },
        error: function (xhr, status, error) {
             console.log( " xhr.responseText: " + xhr.responseText + " //status: " + status + " //Error: "+error );
            alert("error");
        }
    });

It throws error:

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."

2
  • 1
    Not sure if this is the problem here but i'm pretty sure the request type should be POST(or PUT) instead of GET if you want to do write operations.
    – garglblarg
    Commented Jun 30, 2016 at 10:44
  • 1
    I made a little changes to the question,
    – KumarV
    Commented Jun 30, 2016 at 11:37

4 Answers 4

7

Refresh the token in Form Digest

The two most common ways are to either use ye olde web services and call into the /_vti_bin/sites.asmx and use the GetUpdatedFormDigest method. To use this you have to create a SOAP message and then parse the response and retrieve the updated token. You can then either pass that new token for your subsequent requests, or even better update the hidden __REQUESTDIGEST field. The second one is to use the new REST POST endpoint /_api/contextinfo and then parse that response which can be either XML or JSON. This is how to do it the JSON way:

UPDATE:

Use your method like this:

$.ajax({
    url: _spPageContextInfo.webAbsoluteUrl + "/_api/contextinfo",
    method: "POST",
    headers: { "Accept": "application/json; odata=verbose"},
    success: function (data) {

        $.ajax({
        url: appWebUrl + "/_api/SP.AppContextSite(@target)" + "/web/lists/getbytitle('NewList')/items?" + "@target='" + hostWebUrl + "'",
        type: "POST",
        data:  JSON.stringify({ '__metadata': { 'type': 'SP.Data.NewListListItem' },'Title': "TitleVal", 'Name':"Satya"}),
        headers: { 
            "accept": "application/json;odata=verbose",
            "X-RequestDigest": $('#__REQUESTDIGEST').val(data.d.GetContextWebInformation.FormDigestValue),
            "content-Type": "application/json;odata=verbose" },
        success: function (data) {
            success(data);
            },
        error: function (xhr, status, error) {
            console.log( " xhr.responseText: " + xhr.responseText + " //status: " + status + " //Error: "+error );
            alert("error");
                }
            });
    },
    error: function (data, errorCode, errorMessage) {
        alert(errorMessage)
    }
});

Hope this will help you.

6
  • 1
    how would I add "/_api/contextinfo", to my url appWebUrl + "/_api/SP.AppContextSite(@target)" + "/web/lists/getbytitle('NewList')/items?" + "@target='" + hostWebUrl + "'",
    – KumarV
    Commented Jul 1, 2016 at 8:03
  • 1
    Call that method before your method to refresh the token. Commented Jul 1, 2016 at 8:06
  • 1
    I have mentioned url like url: _spPageContextInfo.webAbsoluteUrl + "/_api/contextinfo", but it says _spPageContextInfo is undefined
    – KumarV
    Commented Jul 1, 2016 at 8:13
  • 1
    Load SP.js before calling this method...Or use your appweburl Commented Jul 1, 2016 at 8:16
  • 1
    It still shows the same error
    – KumarV
    Commented Jul 1, 2016 at 12:46
1

Old thread but just want to share what fixed my issue.

Make sure the user performing the REST call have below the permission "User Remote Interfaces". To check the permission level, refer to below steps:

Site settings (root site) > Site Permissions > Permission Levels > Edit Permission Level > Under Site Permissions

Use Remote Interfaces - Use SOAP, Web DAV, the Client Object Model or SharePoint Designer interfaces to access the Web site.

0

The error message indicates that your App doesn't have permissions to do the add. You need to request write permission at the Web scope in the App manifest.

enter image description here

You also need to change the GET to a POST.

0

You can refresh Form Digest Token just before the POST call.

there is a native method for refreshing the token built-in to the SharePoint JavaScript libraries. It’s a function called UpdateFormDigest(). It’s defined in INIT.js.

The method takes two parameters, first of all the URL to the current site and secondly it takes an update interval parameter (global constant called _spFormDigestRefreshInterval ). And this is how you should use the function:

UpdateFormDigest(_spPageContextInfo.webServerRelativeUrl, _spFormDigestRefreshInterval);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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