Sam's Dynamics, Power Platform & AI 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!!!

1 comment:

Display Density in Model-Driven Apps: Less Whitespace, More CRM

 If you work with Dynamics 365, you've heard this one: "Why is there so much empty space on the screen?" Microsoft now has an ...