public static void Test3(string webUri, string endpointUrl, string userName, string password) { using (var client = new SPHttpClient(new Uri(webUri), userName, password)) { var listTitle = "Tasks"; var data = client.ExecuteJson(endpointUrl); foreach (var item in data["value"]) { Console.WriteLine(item["Title"]); } } }
//https://blog.mastykarz.nl/azure-ad-app-only-access-token-using-certificate-dotnet-core/ //http://www.erwinmcm.com/azure-active-directory-app-only-authentication-with-officedev-pnp-powershell/ //Generation du certificat sur Mac : https://blogs.msdn.microsoft.com/arsen/2015/09/18/certificate-based-auth-with-azure-service-principals-from-linux-command-line/ //Generation du pfx sur Mac : openssl pkcs12 -inkey bob_key.pem -in bob_cert.cert -export -out bob_pfx.pfx //Publication Azure Web Job : https://blog.kloud.com.au/2016/06/08/azure-webjobs-with-dotnet-core-rc2/ public static void Main(string[] args) { var tenantId = "TENANT ID OF AUZRE TNEANT"; var resource = "URL OF YOUR SPOL TENANT"; var clientId = "CLIENT ID OF THE AZURE APP"; var accessToken = ServicePrincipal.GetS2SAccessToken("https://login.microsoftonline.com/" + tenantId, resource, clientId).Result; var webUri = new Uri("URL OF THE SITE COLLECTION"); using (SPHttpClient client = new SPHttpClient(webUri, accessToken)) { try { var itemPayload = new { __metadata = new { type = "SP.Data.TestListItem" }, Title = "Element de test" }; var endpointUrl = string.Format("{0}/_api/web/lists/getbytitle('Test')/items", webUri); var item = client.ExecuteJson(endpointUrl, HttpMethod.Post, null, itemPayload); Console.WriteLine("List item created"); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } }
//In this demo we have used Sharepoint Online version 16.1.19404.12000 for DotNet Framework static void Main(string[] args) { #region Credentials // Console.Write("Enter SharePoint Online Admin Site URL: "); string siteUrl = "https://brgrp-admin.sharepoint.com/"; //Console.Write("Enter User Name: "); string userName = CommonCredentials.UserName; // Console.Write("Enter Password: "******"https://brgrp-admin.sharepoint.com/_vti_bin/client.svc/lists/GetByTitle('DO_NOT_DELETE_SPLIST_TENANTADMIN_AGGREGATED_SITECOLLECTIONS')/items?$top=10&$filter=IsGroupConnected"; #region Approach 1 Call REST API using CSOM with SPHttpClient Console.WriteLine("Approach 1 Call REST API using CSOM SPHttpClient\n\n"); var client = new SPHttpClient(rootUri, userName, password); var res = client.ExecuteJson(reqUrl); Console.WriteLine("\nReceived All site collection data\n\n"); Console.WriteLine(res); #endregion SecureString secureString = new SecureString(); foreach (char c in password) { secureString.AppendChar(c); } var credentials = new SharePointOnlineCredentials(userName, secureString); var cookie = credentials.GetAuthenticationCookie(rootUri); //Use SharePoint REST API To Get Admin sites using CookieContainer //Most Essential Part for making REST Call is to use CookieContainer var cc = new CookieContainer(); cc.SetCookies(rootUri, cookie); #region Approach 2 var spClient = new HttpClient(new HttpClientHandler() { CookieContainer = cc }); spClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); Console.WriteLine("Apraoach 2 :Requesting All site collection data uisng HttpClient\n\n"); var groupConnectedSites = spClient.GetStringAsync(reqUrl).GetAwaiter().GetResult(); Console.WriteLine(groupConnectedSites); Console.WriteLine("\n\n Received All site collection data\n\n"); #endregion #region Approach 3 : Call SharePoint Online REST API Using C# Console.WriteLine("Apraoach 3 :Requesting All site collection data with Resuable CallSharePointOnlineAPI method\n\n"); var allSites = CallSharePointOnlineAPI(reqUrl, HttpMethod.Get, null, null, null, cc).GetAwaiter().GetResult(); Console.WriteLine("\nReceived All site collection data\n\n"); Console.WriteLine(allSites); #endregion //#region Approach 3 Call REST API using CSOM //Console.WriteLine("Approach 3 Call REST API using CSOM\n\n"); //var client = new SPHttpClient(rootUri, userName, password); //var res = client.ExecuteJson(reqUrl); //Console.WriteLine("\nReceived All site collection data\n\n"); //Console.WriteLine(res); //#endregion }