Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Get SharePoint Form Digest from C# Code

Without words...

using Microsoft.SharePoint.Client;
using Newtonsoft.Json;
using System;
using System.Net.Http;
using System.Security;
using System.Threading.Tasks;

namespace GetFormDigest
    class Program
            const string SPSITE = "";
            const string USER = "[email protected]";
            const string PWD = "wlamu$$a.7";

            var handler = GetClientHandler(SPSITE,

            var formDigest = GetFormDigest(handler, SPSITE);


        internal class FormDigest
            public class Rootobject
                public int FormDigestTimeoutSeconds { get; set; }
                public string FormDigestValue { get; set; }
                public string LibraryVersion { get; set; }
                public string SiteFullUrl { get; set; }
                public string[] SupportedSchemaVersions { get; set; }
                public string WebFullUrl { get; set; }

        internal static async TaskFormDigest.Rootobject> GetFormDigest(HttpClientHandler handler, string spSite)
            const string RESTURL = "{0}/_api/contextinfo";
            string restUrl = string.Format(RESTURL, spSite);
            var client = new HttpClient(handler);

            client.DefaultRequestHeaders.Add("Accept", "application/json;odata=nometadata");

            HttpResponseMessage response = awaitclient.PostAsync(restUrl, null).ConfigureAwait(false);

            string jsonData = awaitresponse.Content.ReadAsStringAsync();

            FormDigest.Rootobject res = JsonConvert.DeserializeObjectFormDigest.Rootobject>(jsonData);
            return res;

        internal static HttpClientHandler GetClientHandler(string spSiteUrl, string username, string pwd)
            //Creating Credentials
            var passWord = new SecureString();
            foreach (var c in pwd) passWord.AppendChar(c);
            var credential = new SharePointOnlineCredentials(username, passWord);

            HttpClientHandler handler = new HttpClientHandler() { Credentials = credential };

            //Getting authentication cookies
            Uri uri = new Uri(spSiteUrl);
            handler.CookieContainer.SetCookies(uri, credential.GetAuthenticationCookie(uri));

            return handler;


This post first appeared on ZSvipullo, please read the originial post: here

Share the post

Get SharePoint Form Digest from C# Code


Subscribe to Zsvipullo

Get updates delivered right to your inbox!

Thank you for your subscription