1

I have problem with REST Query that should update field in project. I am sending the following Query:

var projectItem = {
    "projectId":  "21dba5cc-43e9-e611-80e2-02bf0a010149",
    "propertyId": '22301607-98b8-e611-80e6-02bf0a010149',
    "value": "Some example Action field value."
};

var projectUpdated = false;
jQuery.ajax({
    url: _spPageContextInfo.siteAbsoluteUrl + '/_api/ProjectServer/WorkflowActivities/updateTextProperty',
    type: 'POST',
    contentType: 'application/json;odata=verbose',
    data: JSON.stringify(projectItem),
    headers: {
        "Accept": 'application/json;odata=verbose',
        "X-RequestDigest": jQuery('#__REQUESTDIGEST').val()
    },
    success: function(data) {
        console.log("jQuery ajax was finished successfully with the following prop: ");
        console.log("    Project id: " + projectItem.projectId + ","); 
        console.log("    Field id: " + projectItem.value);
        console.log("    Field value: " + projectItem.value);
    },
    error: function(data) {
        console.log("jQuery ajax was finished unsuccessfully with the following error:");
        console.log("    data.status: " + data.status);
        console.log("    data.statusText: " + data.statusText);
        console.log("    data.responseText: " + data.responseText);
    }
});

And I get the following error:

jQuery ajax was finished unsuccessfully with ther following error:
    data.status: 403
    data.statusText: FORBIDDEN
    data.responseText: {"error":{"code":"-2130575252, 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."}}}
  • Following Url will help you – nbi Nov 24 '17 at 5:56
1

Try it as below:

var digest;
$.ajax({
        url: _spPageContextInfo.siteAbsoluteUrl + "/_api/contextinfo",
        method: "POST",
        headers: { "Accept": "application/json; odata=verbose" },
        success:function(data){
            digest = data.d.GetContextWebInformation.FormDigestValue;
            updateItem(digest);
        },
        error:function(data){
            console.log("something went wrong")
        }
    });

function updateItem(digest){
    var projectItem = {
        "projectId":  "21dba5cc-43e9-e611-80e2-02bf0a010149",
        "propertyId": '22301607-98b8-e611-80e6-02bf0a010149',
        "value": "Some example Action field value."
    };

    var projectUpdated = false;
    jQuery.ajax({
        url: _spPageContextInfo.siteAbsoluteUrl + '/_api/ProjectServer/WorkflowActivities/updateTextProperty',
        type: 'POST',
        contentType: 'application/json;odata=verbose',
        data: JSON.stringify(projectItem),
        headers: {
            "Accept": 'application/json;odata=verbose',
            "X-RequestDigest": $('#__REQUESTDIGEST').val(digest)
        },
        success: function(data) {
            console.log("jQuery ajax was finished successfully with the following prop: ");
            console.log("    Project id: " + projectItem.projectId + ","); 
            console.log("    Field id: " + projectItem.value);
            console.log("    Field value: " + projectItem.value);
        },
        error: function(data) {
            console.log("jQuery ajax was finished unsuccessfully with the following error:");
            console.log("    data.status: " + data.status);
            console.log("    data.statusText: " + data.statusText);
            console.log("    data.responseText: " + data.responseText);
        }
    });
}

Reference - Refresh request digest in javascript

  • Still return the same error. – TomaszP Feb 21 '17 at 12:09
  • are you using App ? – Gautam Sheth Feb 21 '17 at 12:28
  • No, I am using script editor web part. – TomaszP Feb 21 '17 at 12:48
  • Instead of jQuery('#__REQUESTDIGEST').val() can you use document.getElementById("__REQUESTDIGEST").value and check ? – Gautam Sheth Feb 21 '17 at 12:54
  • Both functions returns exactly the same digit. – TomaszP Feb 21 '17 at 14:47
1

looks like your request token has changed:

jQuery('#__REQUESTDIGEST').val()

run this before the code above:

if (Sys.Net.WebRequestManager._events != null && Sys.Net.WebRequestManager._events._list != null) 
{ 
    var invokingRequests = Sys.Net.WebRequestManager._events._list.invokingRequest; 
    while( invokingRequests != null && invokingRequests.length > 0) 
    { 
        Sys.Net.WebRequestManager.remove_invokingRequest(invokingRequests[0]); 
    } 
}

looks like your token has changed giving a 403

403 Error on REST/JSOM call within a form in SharePoint 2013

  • It returns: ReferenceError: data is not defined. – TomaszP Feb 21 '17 at 12:09
  • just spotted the mistake. you need to remove the # , check my updated answer... – Ali Jafer Feb 21 '17 at 12:39
  • This return's undefined. Function with # reutrn's the actual digit, but overall ajax query still gives me 403. – TomaszP Feb 21 '17 at 12:51
  • i think your request token has changed as that should work.... please check out the updated answer – Ali Jafer Feb 21 '17 at 13:23
  • I used this code and there are no invoking request. Object is downloaded correctly, but length is equal to zero. – TomaszP Feb 21 '17 at 14:35

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.