Sam's Microsoft Dynamics 365 Blog

Tuesday, 24 September 2019

Form Digest issue in SharePoint REST API

In this article we will discuss about SharePoint Form Digest issue in REST API. At the time of POST event in SharePoint REST API, we have to pass  "X-RequestDigest": jQuery("#__REQUESTDIGEST").val();  but it seems, some time jQuery("#__REQUESTDIGEST").val(); returns noting(undefined).



 So, in this case we can use below code to get Form Digest and can use in our JavaScript code instead of jQuery("#__REQUESTDIGEST").val();


   var hostUrl = "http://mycompany.sharepoint.com/sites/site1/SharePoint/oAuth";
   function getFormDigest() {
    $.support.cors = true;
    var _formDigest;
    $.ajax({
        url: hostUrl + "/_api/contextinfo",
        type: "POST",
        dataType: "json",

      headers: { "Accept": "application/json; odata=verbose" },
        xhrFields: { withCredentials: true },
        async: false,
        success: function (data) {
            _formDigest = data.d.GetContextWebInformation.FormDigestValue;
        },
        error: function () {
            console.log("Error Occured");
         }
    });

    return _formDigest;
   }



 Thanks for reading the article. Hope this Article will helpful for you. Cheers!!!

Before you switch Copilot on, spend a week in your data. Here's what I'd check.

If you're about to switch Copilot on, spend a week in your data first. None of what follows is exciting. That's rather the point. ...