Skip to content

Instantly share code, notes, and snippets.

@dev-kperera
Created May 16, 2016 16:52
Show Gist options
  • Save dev-kperera/3c9eb56ce7967177191cb36d8571d8c1 to your computer and use it in GitHub Desktop.
Save dev-kperera/3c9eb56ce7967177191cb36d8571d8c1 to your computer and use it in GitHub Desktop.
Angular Js service to get SharePoint Form Digest value (X-RequestDigest) value
(function(){
'use strict'
// Service that returns form digest value
angular
.module('app')
.service('getFormDigestService', function($http, $q){
//Define the service call
this.getFormDigest = function(){
//Fetch the Url
JSRequest.EnsureSetup();
var appweburl = decodeURIComponent(JSRequest.QueryString['SPAppWebUrl']);
//define promise
var d = $q.defer();
//Http post to the context info rest service
$http.post(appweburl + "/_api/contextinfo", {
data: '',
headers: {
"Accept": "application/json;odata=verbose",
"Content-Type": "application/json;odata=verbose"
},
}).success(function (data) {
//resolve the form digest to promise
d.resolve(data.d.GetContextWebInformation.FormDigestValue);
}).error(function (err) {
d.reject("error finding form digest");
console.log("Error on retrieving digest value");
});
return d.promise;
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment