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!!!

Web Resource vs PCF vs Canvas App - which of the one is used?

While started working on specific Business Requirements related to custom layout, there is a always common question that "where to star...