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
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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!
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
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
$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
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.
Refer below link to connect SharePoint using Active Directory OAuth.