
Document Sets in SharePoint Online allow you to manage related documents as a single unit. Using PnP PowerShell, we can:
 Automate document set creation
 Configure metadata and permissions
 Manage workflows for document approvals
Step 1: Connect to SharePoint Online
Connect to your SharePoint Online site using PnP PowerShell:
$siteUrl = "https://yourtenant.sharepoint.com/sites/YourSite"
Connect-PnPOnline -Url $siteUrl -Interactive
Write-Host " Connected to SharePoint Online"
✔ Establishes a secure connection.
Step 2: Create a Document Set Content Type
Before using document sets, create a content type:
$docSetName = "ProjectFiles"
$groupName = "Custom Content Types"
Add-PnPContentType -Name $docSetName -Description "Document Set for Projects" -Group $groupName -ParentContentType "Document Set"
Write-Host " Document Set Content Type '$docSetName' created"
✔ Ensures structured document management.
Step 3: Add the Document Set to a Library
Apply the document set content type to a library:
$libraryName = "Project Documents"
Add-PnPContentTypeToList -List $libraryName -ContentType $docSetName
Write-Host " Document Set added to '$libraryName'"
✔ Enables document set functionality in a library.
Step 4: Automate Document Set Creation
Create a document set programmatically:
$docSetTitle = "Project A"
$libraryUrl = "Project Documents"
New-PnPListItem -List $libraryUrl -ContentType $docSetName -Values @{"Title" = $docSetTitle}
Write-Host " Document Set '$docSetTitle' created"
✔ Automates document set generation.
Step 5: Assign Metadata and Permissions
Set metadata and unique permissions for a document set:
$docSetItem = Get-PnPListItem -List $libraryUrl -Query "<View><Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>$docSetTitle</Value></Eq></Where></Query></View>"
Set-PnPListItem -List $libraryUrl -Identity $docSetItem.Id -Values @{"ProjectStatus"="Active"}
Set-PnPListItemPermission -List $libraryUrl -Identity $docSetItem.Id -User "user@domain.com" -AddRole "Contribute"
Write-Host " Metadata and permissions assigned"
✔ Ensures data consistency and security.
Step 6: Automate Approval Workflows
Trigger Power Automate workflows using PnP PowerShell:
$flowName = "ApproveProjectDocuments"
Start-PnPFlow -List $libraryUrl -FlowName $flowName -Parameters @{"DocumentSetTitle"=$docSetTitle}
Write-Host " Workflow triggered for approval"
✔ Integrates Power Automate for approvals.
Step 7: Monitor and Manage Workflows
Check workflow status:
Get-PnPFlowRun -List $libraryUrl -FlowName $flowName | Select-Object Status, Started
✔ Provides real-time workflow tracking.
Step 8: Automate Cleanup of Completed Workflows
Remove completed workflows to optimize performance:
Remove-PnPFlowRun -List $libraryUrl -FlowName $flowName -RunId "workflow-run-id"
Write-Host " Cleaned up completed workflow runs"
✔ Ensures efficient workflow execution.
