How to connect SharePoint online using service principal?

Khushboo Kumari 92 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!

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,611 questions
Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,118 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,650 questions
SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,573 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Sedat SALMAN 13,160 Reputation points
    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. ChengFeng - MSFT 5,010 Reputation points Microsoft Vendor
    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 20 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