How to connect SharePoint online using certificate?

Khushboo Kumari 107 Reputation points
2023-06-27T18:55:35.22+00:00

Hi

I want to execute my code using azure runbook. I am trying to connect the SharePoint using below command

$application = ""

$certificateThumbprint = ""

Connect-PnPOnline -ClientId $application -Thumbprint $certificateThumbprint -Url "https://testmig097.sharepoint.com/sites/site2" -Tenant "testmig097.onmicrosoft.com"

but getting error like this User's image

User's image

How to resolve it? and is there any other method to connect SharePoint in azure runbook?

Microsoft 365 and Office SharePoint Server For business
Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,366 questions
Microsoft 365 and Office SharePoint For business Windows
Windows for business Windows Server User experience PowerShell
Microsoft Security Microsoft Graph
{count} votes

1 answer

Sort by: Most helpful
  1. AnuragSingh-MSFT 21,546 Reputation points Moderator
    2023-06-29T04:57:00.0566667+00:00

    @Khushboo Kumari , thank you for posting this question here. I see that you are getting the "Keyset does not exist" error, when trying to connect to SharePoint online using the Connect-PnPOnline cmdlet.

    Generally, this error specifically points to the following 2 issues:

    1. The certificate used/uploaded does not have private key associated with it. OR
    2. That the user account (which is executing the runbook, in this case the Azure Automation job account) does not have access to the certificate.

    Since the issue is happening in Azure Automation, you cannot do much about the 2nd point mentioned above. Therefore, please ensure that the certificate (whose thumbprint is being used in the cmdlet, does have a private ket associated with it).

    Please follow the step mentioned below, to ensure that correct type of certificate has been created and it is uploaded to AppRegistration with the private key with it - Granting access via Azure AD App-Only.

    If your sharepoint and Azure Automation account are in the same tenant, you can also use Managed Identity to connect to Sharepoint online from Azure Automation. This is much cleaner and does not require certificate based authentication. For details, see Authentication in Azure Automation with Managed Identity on SharePoint and Microsoft Graph

    Also, see the guidelines from the official PnP repository: Connect to the SharePoint Online using Application Permissions

    Hope this helps.

    1 person found this answer helpful.

Your answer

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