Automate SharePoint Document Set Configuration with PowerShell
Introduction
Document Sets in SharePoint are a powerful way to manage groups of related documents as a single entity. They enable you to apply metadata, workflows, and permissions to a collection of documents, making them ideal for project folders, case files, or any scenario where you need to keep related content together.
This post shows how to automate the configuration of Document Sets across multiple libraries using PnP PowerShell.
Why Use Document Sets?
- Centralized Management: Group related documents together for easier management.
- Consistent Metadata: Apply metadata to the entire set and its contents.
- Improved Search & Navigation: Quickly find all documents related to a project or case.
- Workflows & Permissions: Apply workflows and permissions at the set level.
For a deeper dive into Document Set benefits and use cases, check out these resources:
Automate Document Set Configuration with PowerShell
The following PnP PowerShell script will:
- Enable the Document Set feature
- Create a custom Document Set content type
- Add site columns to the content type
- Add the content type to all document libraries (excluding system libraries)
- Create sample document sets and set metadata
- Add columns to the default view
param (
[Parameter(Mandatory = $true)]
[string] $siteUrl,
[Parameter(Mandatory = $true)]
[string] $docsetCTName,
[Parameter(Mandatory = $true)]
[string] $columnsToAddToDocSet = "Company,Department",
[Parameter(Mandatory = $false)]
[string] $docSetToAdd = "CompanyA,CompanyB"
)
Connect-PnPOnline -Url $siteUrl
# Activate Document Set feature
Enable-PnPFeature -Identity "3bae86a2-776d-499d-9db8-fa4cdc7884f8" -Scope Site -ErrorAction SilentlyContinue
# Ensure parent content type is available
$parentContentType = $null
while($null -eq $parentContentType ) {
$parentContentType = Get-PnPContentType -Identity "Document Set"
Start-Sleep -Seconds 5
}
# Create custom Document Set content type
Add-PnPContentType -Name $docsetCTName -ParentContentType $parentContentType -Group "Doc Set Content Types" -ErrorAction SilentlyContinue | Out-Null
# Add columns to content type
$columnsToAddToDocSet.Split(",") | ForEach-Object {
Add-PnPFieldToContentType -Field $_ -ContentType $docsetCTName | Out-Null
}
# Exclude system libraries
$ExcludedLists = @("Access Requests", "App Packages", ... ) # (truncated for brevity)
Get-PnPList | Where-Object { $_.BaseTemplate -eq 101 -and $_.Hidden -eq $False -and $_.Title -notin $ExcludedLists } | ForEach-Object {
$list = Get-PnPList -Identity $_
Set-PnPList -Identity $list -EnableContentTypes $True
Add-PnPContentTypeToList -List $list -ContentType $docsetCTName | Out-Null
Set-PnPDefaultContentTypeToList -List $list -ContentType $docsetCTName
# Create document sets and set metadata
$docSetToAdd.Split(",") | ForEach-Object {
$docSetName = $_
$docSet = Add-PnPDocumentSet -List $list -ContentType $docsetCTName -Name $docSetName
$docSetItem = Get-PnPListItem -List $list -Query "<View><Query><Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='Text'>$docSetName</Value></Eq></Where></Query></View>"
Set-PnPListItem -List $list -Identity $docSetItem.Id -Values @{Company="Company A"; Department="Finance"; } | Out-Null
Write-Host "Document set '$docSetName' created and metadata set."
}
# Add columns to default view
$DefaultListView = Get-PnPView -List $list | Where-Object { $_.DefaultView -eq $True }
$columnsToAddToDocSet.Split(",") | ForEach-Object {
if ($DefaultListView.ViewFields -notcontains $_) {
try {
$DefaultListView.ViewFields.Add($_)
$DefaultListView.Update()
Invoke-PnPQuery
Write-Host -f Green "$_ column added to the Default View in library $($list.Title)!"
} catch {
Write-Host -f Red "Error adding $_ column to the View! $($list.Title)"
}
}
}
}
Output
Conclusion
Document Sets are alternatives to folders for organizing and managing related documents in SharePoint. By automating their configuration with PowerShell, you can ensure consistency and save time across your sites and libraries. For more best practices and real-world scenarios, be sure to check out the resources from SharePoint Maven and Anderson.