0

I am trying to make a POST request to add a new item to a SharePoint list in SharePoint online, from a classic page. I am having this code in my custom action:

    var url = location.href
    var initiativeName = $("#idDocsetName").text()

    var target = "https://tenantname/sites/sitename/_api/web/lists/getByTitle('Requests Approvals')/items"
    var data = '{ "__metadata": { "type": "SP.Data.Requests_x0020_ApprovalsListItem" }, "Title":"'+ requestName+'", "requestURL":"'+  url +'", "ManagerEmail":"'+  email+'"}'
    $.ajax({
        url: target, 
        type: "POST", 
        contentType: "application/json;odata=verbose",
        data: data,
        headers: {
             "Accept": "application/json;odata=verbose",  
             "X-RequestDigest": $("#__REQUESTDIGEST").val(), 
             "content-Type": "application/json;odata=verbose"
        }
    })

I am getting 403 error. How can I know more what's needed? when I click on the request details, I find this:

enter image description here

2 Answers 2

1

It seems you are running your code from different site page and trying to add item to list which is in diffrent site...I assumed this because you are building your target url with absolute url.. if that is case, it won't work. 403 forbidden means you are not allowed to access that resource.

You are getting this error because RequestDigest token which you have on-page is for current site and not the target site... To do this, you first have to get digest token and use that token to make the request to a remote site collection.

You need to first call _api/contextinfo on remote site and retrieve the FormDigestValue Use this form digest value to make post request.

0

If GET works but POST does not, then most likely it is an issue with your X-RequestDigest value. In troubleshooting these issues it is helpful to use Postman to validate your SharePoint REST API call values/syntax is valid.

https://medium.com/@anoopt/accessing-sharepoint-data-using-postman-sharepoint-rest-api-76b70630bcbf

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.