0

Is there any way to connect SharePoint Rest API with Client Credentials in C# to get site classifications? I previously used Graph API to get those collections, but I need the same using SharePoint Rest API.

 IConfidentialClientApplication CCA = ConfidentialClientApplicationBuilder
              .Create(c_Id).WithTenantId(t_Id).WithClientSecret(clientSecret).Build();
 ClientCredentialProvider CCP = new ClientCredentialProvider(CCA);
 GraphServiceClient g_Client = new GraphServiceClient(CCP);
 var Sites = await g_Client.Sites.Request().GetAsync();

I used the above code to work with Graph API, How to do the same with SharePoint rest API?

1 Answers1

2

SharePoint Online has blocked Azure AD App Client Secret besides certificates:

enter image description here

So it's necessary to create a self signed certificiate and upload for the Azure AD App:

enter image description here

For creating the certificate, please follow the official document, create using PowerShell:

Granting access via Azure AD App-Only

In C# code firstly, create a class named TokenProvider for example and fill with this code snippet:

using Microsoft.Identity.Client;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;

     class TokenProvider
        {
            public static async Task<string> GetAccessTokenAsync(string endpoint)
            {
                var clientId = "yourclientId";
                var tenantId = "yourtenantId";
                
                var certificate = GetCertificate(Path.Combine("D:\\", "certificatename.pfx"), "certificatepwd");
                var confidentialClient = ConfidentialClientApplicationBuilder.Create(clientId).WithTenantId(tenantId).WithCertificate(certificate).Build();
               
                var token = await confidentialClient.AcquireTokenForClient(new[] { $"{endpoint.TrimEnd('/')}/.default" }).ExecuteAsync();
    
                return token.AccessToken;
            }
    
            private static X509Certificate2 GetCertificate(string path, string password)
            {
                return new X509Certificate2(path, password, X509KeyStorageFlags.MachineKeySet);
            }
        }

Then in the Main() function call like this:

       var siteUrl = "https://tenantname.sharepoint.com/";

        var token = await TokenProvider.GetAccessTokenAsync(new Uri(siteUrl).GetLeftPart(UriPartial.Authority));

        var url = "https://tenantName.sharepoint.com/_api/search/query?querytext='contentclass:sts_site'";
        
        var client = new WebClient();
        client.Headers[HttpRequestHeader.Accept] ="application/json;odata=verbose";
        client.Headers[HttpRequestHeader.ContentType] ="application/json;odata=verbose";
        client.Headers[HttpRequestHeader.Authorization] = "Bearer " + token;
        var json = client.DownloadString(url);
Jerry
  • 3,480
  • 1
  • 10
  • 12
  • Thank you, I tried the above steps and It works. I wanna use delta sync in SharePoint rest API, Does it support delta sync? – Nithish Kumar Apr 28 '21 at 11:20
  • 1
    Sure it support to download string async with await: string data = await client.DownloadStringTaskAsync(uri); https://stackoverflow.com/questions/13240915/converting-a-webclient-method-to-async-await – Jerry Apr 28 '21 at 13:01
  • @Jerry_MDFT I want only the recent changes made in a site, In graph API I used /Delta endpoint to get only the changes instead of the whole site collection. Can I do the same with Sharepoint Rest API? – Nithish Kumar Apr 29 '21 at 03:40
  • 1
    In Rest API, there is no delta data enpoint and concept for the site collection data. So this is not available when using Rest API. – Jerry Apr 29 '21 at 05:52