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 = "https://zsis01.sharepoint.com";
            const string USER = "[email protected]";
            const string PWD = "wlamu$$a.7";

            var handler = GetClientHandler(SPSITE,
                                             USER,
                                             PWD);

            var formDigest = GetFormDigest(handler, SPSITE);
            formDigest.Wait();

            Console.WriteLine(formDigest.Result.FormDigestValue);
        }

        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.Accept.Clear();

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

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

            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

×