1

My app page doesn't have __REQUESTDIGEST on page elements. How do I get this value from sharepoint API ? I used this codes but return 403 Forbidden error

var formDigest;
    $.ajax({
        url: hostUrl + "/_api/contextinfo",
        method: "POST",
        headers: { "Accept": "application/json; odata=verbose" },
        success: function (data) {
            //$('#__REQUESTDIGEST').val(data.d.GetContextWebInformation.FormDigestValue)
            formDigest = data.d.GetContextWebInformation.FormDigestValue
        },
        error: function (data, errorCode, errorMessage) {
            alert(errorMessage)
        }
    });

1 Answer 1

1

If you are authenticated then it is:

_spPageContextInfo.webAbsoluteUrl + '/_api/contextinfo'

All Modern Experiences do not have that hidden #__REQUESTDIGEST either, so you have to call the /contextinfo endpoint

I suggest to use a window.setInterval function to always make sure you have a valid digest in a global variable.
Saves you from requesting a new value before every REST call and thus simplifies your code.

From an App:

https://stackoverflow.com/questions/22159609/how-to-get-request-digest-value-from-provider-hosted-app

3
  • First thanks for your comment. I'm authenticated. I retrieve all of lists. But when I'm trying to get digest it returns forbidden error. Do you think webAbsoluteUrl is the same Sharepoint Host url ? Like project.sharepoint.com/sites/appsite ?? Nov 4, 2016 at 8:57
  • Ah, are you working from an App and not straight from the Host Web? You then need to go through the request executor, I don't have that code at hand. Nov 4, 2016 at 8:59
  • Yes I'm working on App. But thanks any way. I'm searching request executor now. Nov 4, 2016 at 9:32

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.