10

I am trying to delete a web by calling the REST service from a SharePoint 2013 workflow. The MSDN reference here tells the syntax is like below:

$.ajax({
  url: "http://<site url>/_api/web",
  type: "POST",
  headers: { 
    "X-RequestDigest": <form digest value>,
    "X-HTTP-Method": "DELETE"
  },
  success: successHandler,
  error: errorHandler
});

But, I am not famililiar how to get 'form digest value'.

Can anyone assist on this?

1

3 Answers 3

9

You should rename the question to "How to get form digest value"...

There are two ways to get the form digest value:

  1. Get the form digest value from the page: document.getElementById("__REQUESTDIGEST").value
  2. Invoke a post request to get the context info

    $.ajax({url: "<site_url>/_api/contextinfo", 
       header: {
            "accept": "application/json; odata=verbose", 
            "content-type":"application/json;odata=verbose"}, 
            type: "POST", 
            contentType: "application/json;charset=utf-8"
     }).done(function(d) {
             //run your code
          });
    
1
  • 1
    I guess the OP is asking about solution for SPD Workflow.. Commented Sep 8, 2014 at 13:10
1

You can do this in SPD but it is a little less straightforward than using jQuery.

The below page has a tutorial that I can confirm still works with SharePoint Server 2016.

The gist of that page is reproduced here:

  1. Create Build new Dictionary action outputting to a Dictionary called header

  2. Create two keys in header with these values:

    • Accept: application/json; odata=verbose
    • Content-Type: application/json; odata=verbose
  3. Add a Call HTTP Web Service action and configure like so:

    • Url: [%site url%]_api/contextinfo
    • Type: POST
    • Request Header: header
    • Response Content: ResponseContent (new Dictionary variable)
  4. Add a Get Item from Dictionary activity & configure like so:

    • Path: d/GetContextWebInformation/FormDigestValue
    • From: ResponseContent
    • Output: DigestID (new String variable)

This will give you a String variable containing the Request Digest ID, which you can then use in subsequent HTTP POST actions.

Source: https://saberghanmien.wordpress.com/2018/06/06/sharepoint-designer-call-http-web-service-with-requestdigest/

0

This is the solution I use in my angular js application...I have 2 services that I use...one gets the form digest value, the other runs on a 24 minute interval on the $rootScope and continuously updates the FormDigestValue no matter how long someone is inactive (so they don't attempt to post when they come back and get an error). You then inject the $rootScope.formDigestValue into any POST calls in AJAX in the header for "X-RequestDigest" property. The formDigestIntervalService is started upon application loading (or in the routeing script if using angular routes) by calling:

//start the form digest service
        formDigestIntervalService.startFormDigestInterval($rootScope);

These services sit in a globalModule

 //The service that retreives the Form Digest Value
    .service('formDigestService', function($http, $q){
        //Define the headers
        $http.defaults.headers.common.Accept = "application/json;odata=verbose";
        $http.defaults.headers.post['Content-Type'] = 'application/json;odata=verbose';

        //Define the service call
        this.getFormDigest = function () {
            //Fetch the Url 
            JSRequest.EnsureSetup();
            var appweburl = decodeURIComponent(JSRequest.QueryString['SPAppWebUrl']);
            var dfd = $q.defer();
            //Http post to the context info rest service
            $http.post(appweburl + "/_api/contextinfo", {
                data: '',
                headers: {
                    "Accept": "application/json;odata=verbose",
                    "Content-Type": "text/xml"
                }
            }).success(function (data) {
                //Resolve the FormDigestValue from the success callback.
                console.log('X-DIGEST: ' + data.d.GetContextWebInformation.FormDigestValue);
                dfd.resolve(data.d.GetContextWebInformation.FormDigestValue);
            }).error(function () {
                dfd.reject("error finding form digest");
            });
            return dfd.promise;
        }; 
    })

    //The interval service that sets the Form Digest Value to the $rootScope.
    //This service will be injected with the formDigestService
    .service('formDigestIntervalService', ['formDigestService', function ($formDigestService) {
        var self = this;
        self.startFormDigestInterval = function ($rootScope) {
            //Define a property on the rootScope called formRequestDigest
            if (!$rootScope.formRequestDigest) {
                $rootScope.formRequestDigest = "";
            }
            //Set the Form Digest Value to the $rootScope every 24 min
            $formDigestService.getFormDigest().then(function (digest) {
                $rootScope.formRequestDigest = digest;
                setTimeout(function () {
                    self.startFormDigestInterval($rootScope);
                }
                    , 1000000);
                return false;
            }, function (error) {
                console.error("Error retrieving form digest value: ", error);
            });
        };
    }])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.