4

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

  • 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 Jun 30 '16 at 10:44
  • I made a little changes to the question, – KumarV Jun 30 '16 at 11:37
6

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.

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

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.