Fix for __REQUESTDIGEST value undefined issue- SharePoint Hosted App / Regular Page without Master Page

If you did not set master page on your  SharePoint pages(example: Angualr JS based page), your will get undefined error when you access $(“#__REQUESTDIGEST”).val(). To get digest value use the below code so that this can be passed when you call SharePoint REST APIs.

var formDigest; // Global Variable

$.ajax({
 url: <Pass App Web or Host Web Url based on requirement> + "/_api/contextinfo",
 type: "POST",
 headers: {
 "accept": "application/json;odata=verbose",
 "contentType": "text/xml"
 },
 success: function (data) {
 var requestdigest = data;
 formDigest = data.d.GetContextWebInformation.FormDigestValue;
 },
 error: function (err) { 
 console.log(JSON.stringify(err));
 }
 });
Advertisements

About Joseph Velliah
As a SharePoint Developer my professional interests tend to be technical and SharePoint focused. I run a blog at "SP RIDER" where you can expect to read HOW TOs and scenarios that I run into during my day to day job. I hope my posts will give back a little to the community that is helped me.

Comments are closed.

%d bloggers like this: