How to connect SharePoint online using service principal?

Khushboo Kumari 107 Reputation points
2023-06-29T05:43:07.29+00:00

Hi,

I would like to know how to connect SharePoint online using service principal in azure runbook to create folder and upload the document.

Thanks!

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
Microsoft 365 and Office | SharePoint Server | Development
Microsoft Security | Microsoft Graph
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Sedat SALMAN 14,180 Reputation points MVP
    2023-06-29T06:18:20.16+00:00

    You can follow the steps in this link

    https://www.sharepointdiary.com/2019/03/connect-pnponline-with-appid-and-appsecret.html

    and after connecting to sharepoint you can execute the commands you want

    0 comments No comments

  2. Anonymous
    2023-06-29T06:57:45.2466667+00:00

    Hi @Khushboo Kumari

    Do you want to know how to connect SharePoint online using service principal in azure runbook to create folders and upload documents?

    Here is a link on how to configure:

    https://www.stadlersoftware.com/powershell/sharepoint-online-with-azure-runbooks/

    For you want to implement create folder and upload the document, please refer to the following code and how to use power shell to implement operations on files

    Connect to SharePoint Online using Service Principal

    $TenantId = "<your-tenant-id>"
    $ClientId = "<your-client-id>"
    $ClientSecret = "<your-client-secret>"
    $SiteURL = "<your-sharepoint-site-url>"
    # Load SharePoint CSOM Assemblies
    Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.SharePoint.Client.dll"
    Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.SharePoint.Client.Runtime.dll"
    # Connect to SharePoint Online
    $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $credentials = New-Object Microsoft.SharePoint.Client.ClientCredential($ClientId, $ClientSecret)
    $ctx.Credentials = $credentials
    # Example: Create Folder
    $folderName = "New Folder"
    $list = $ctx.Web.Lists.GetByTitle("Documents")
    $ctx.Load($list.RootFolder)
    $ctx.ExecuteQuery()
    $folder = $list.RootFolder.Folders.Add($folderName)
    $ctx.ExecuteQuery()
    Write-Host "Folder created successfully."
    # Example: Upload Document to Folder
    $filePath = "C:\Path\to\Document.docx"
    $fileName = "Document.docx"
    $fileContent = [System.IO.File]::ReadAllBytes($filePath)
    $uploadFile = $folder.Files.Add($fileName, $fileContent, $true)
    $ctx.Load($uploadFile)
    $ctx.ExecuteQuery()
    Write-Host "Document uploaded successfully."
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    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.

    Best Regards

    Cheng Feng

    0 comments No comments

  3. Suresh Kumar Chermadurai 40 Reputation points
    2023-10-30T11:16:24.43+00:00

    Hi,

    You can create a new app registration and give access to SharePoint using PnP Powershell script. Then finally connect to a SharePoint site using the App details without any MFA. Refer below link to create Service Principal and give access to SharePoint.

    https://www.leonarmston.com/2022/01/pnp-powershell-csom-now-works-with-sharepoint-sites-selected-permission-using-azure-ad-app/

    Refer below link to connect SharePoint using Active Directory OAuth.

    https://spblog.net/post/2021/06/08/how-to-call-sharepoint-rest-api-with-application-permissions-from-azure-logic-app-with-azure-key-vault-and-managed-identity

    0 comments No comments

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.