Get a Certificate authenticated SharePoint context to run Context.Site.CreateSPAsyncReadJob()

Vish Pothaganti 66 Reputation points
2021-04-26T12:49:47.023+00:00

Hello,

I was able to successfully create a SPAsyncReadJob and get the manifest.xml file created on azure using the sample here https://gist.github.com/techmadness/484e7de0a7c51e5faf952a79f1eacb85

I then tried to get the context without UserName/Password and instead used a Certificate I uploaded to the same Azure application like this:
var clientContext = authenticationManager.GetAzureADAppOnlyAuthenticatedContext(SiteURL, ClientId, TenantId, Cert, AzureEnvironment.Production);

The context gets created fine but I get a 401 Unauthorized when trying to create the SPAsyncReadJob on the site.

Can you please advice how to get a authenticated context to create SPAsyncReadJob without using UserName/Password and only by using ClientID/ClientSecret/Certificate.

Microsoft 365 and Office | SharePoint | For business | Windows
0 comments No comments
{count} votes

Answer accepted by question author
  1. ZhengyuGuo 10,591 Reputation points Moderator
    2021-04-27T08:48:29.763+00:00

    Hi @Vish Pothaganti ,

    Instead of Azure AD application, please use the SharePoint application which register in this url:

    https://siteurl/_layouts/15/appregnew.aspx

    91702-snipaste-2021-04-27-16-29-18.png

    And then grant Full Control permission in SharePoint administration site:

    https://tenantname-admin.sharepoint.com/_layouts/15/appinv.aspx

    91694-sharepointapponly2.png

    Click Trust:

    91695-sharepointapponly3.png

    Then in code snippet, use OfficeDevPnP.Core.AuthenticationManager().GetAppOnlyAuthenticatedContext(siteUrl,clientId,clientSecret) function:

                string siteUrl = "your siteurl";  
                string clientId = "your client Id";  
                string clientSecret = "your client secret";  
      
                using (var ctx = new OfficeDevPnP.Core.AuthenticationManager().GetAppOnlyAuthenticatedContext(siteUrl,clientId,clientSecret))  
                {  
                    
      
                };  
    

    Detailed steps to register app and grant permission, please refer this official document, Permission is necessary to set, otherwise, it will also return 401 unauthorized:

    Granting access using SharePoint App-Only

    Thanks
    Best Regards


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.