Créer un site internet

Csom Context Sharepoint ClientContext

fredericdietrich By On 23/05/2023

In SharepointCode

c# Connect to Sharepoint with ClientContext appId and certificat ThumbPrint

 


using Microsoft.Identity.Client;
using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace MY.PNP.Powershell.EXT.CSOM
{
    public class CsomContext : IDisposable
    {
        public ClientContext _clientContext;
        public ClientRuntimeContext _context;

        private async Task GetAccessToken(string tenantName, string clientId, string thumbprint)
        {
            var certificate = GetCert(thumbprint);
            var authority = $"https://login.microsoftonline.com/{tenantName}.onmicrosoft.com/";
            var azureApp = ConfidentialClientApplicationBuilder.Create(clientId)
                .WithAuthority(authority)
                .WithCertificate(certificate)
                .Build();

            var scopes = new string[] { $"https://{tenantName}.sharepoint.com/.default" };
            var authResult = await azureApp.AcquireTokenForClient(scopes).ExecuteAsync();
            return authResult.AccessToken;
        }

        public async Task CallClientObjectModel(string tenantName, string url, string clientId, string thumbprint)
        {
            var token = await GetAccessToken(tenantName, clientId, thumbprint);
            var siteUrl = url;//;$"https://{tenantName}.sharepoint.com";

            var context = new ClientContext(siteUrl);

            context.ExecutingWebRequest += (s, e) =>
            {
                e.WebRequestExecutor.RequestHeaders["Authorization"] =
                    "Bearer " + token;
            };

            var web = context.Web;
            context.Load(web);
            context.ExecuteQuery();
            Console.WriteLine(web.Title);
            Console.WriteLine(web.Url);
            _context = web.Context;
            _clientContext = context;
        }

        private X509Certificate2 GetCert(string thumbprint)
        {
            X509Store certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
            certStore.Open(OpenFlags.ReadOnly);
            X509Certificate2Collection certCollection = certStore.Certificates.Find(
                                       X509FindType.FindByThumbprint,
                                         // Replace below with your cert's thumbprint
                                         thumbprint,
                                       false);
            X509Certificate2 cert = null;
            // Get the first cert with the thumbprint
            if (certCollection.Count > 0)
            {
                cert = certCollection[0];
                // Use certificate
                Console.WriteLine(cert.FriendlyName);
            }
            certStore.Close();
            return cert;
        }

        public void Dispose()
        {
            if (_context != null)
                _context.Dispose();
            if (_clientContext != null)
                _clientContext.Dispose();
        }
    }
}


 

 

CSOM Sharepoint

  • No ratings yet - be the first to rate this.