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
{
static void Main(string[] args)
{
const string SPSITE = "https://zsis01.sharepoint.com";
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;
}
}
}