Skip to content

Commit 62bfbd4

Browse files
authored
Merge pull request #836 from reshmee011/docset
New sample script to configure document set feature.
2 parents 0863268 + 68e21ea commit 62bfbd4

File tree

4 files changed

+173
-0
lines changed

4 files changed

+173
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Configure SharePoint Document Set
2+
3+
## Summary
4+
5+
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.
6+
7+
This script automates the configuration of Document Sets across multiple libraries:
8+
9+
- Enable the Document Set feature
10+
- Create a custom Document Set content type
11+
- Add site columns to the content type
12+
- Add the content type to all document libraries (excluding system libraries)
13+
- Create sample document sets and set metadata
14+
- Add columns to the default view
15+
16+
### Prerequisites
17+
18+
- The user account that runs the script must have access to the SharePoint Online site.
19+
20+
# [PnP PowerShell](#tab/pnpps)
21+
22+
```powershell
23+
param (
24+
[Parameter(Mandatory = $true)]
25+
[string] $siteUrl,
26+
[Parameter(Mandatory = $true)]
27+
[string] $docsetCTName,
28+
[Parameter(Mandatory = $true)]
29+
[string] $columnsToAddToDocSet = "Company,Department",
30+
[Parameter(Mandatory = $false)]
31+
[string] $docSetToAdd = "CompanyA,CompanyB"
32+
)
33+
34+
Connect-PnPOnline -Url $siteUrl
35+
36+
# Activate Document Set feature
37+
Enable-PnPFeature -Identity "3bae86a2-776d-499d-9db8-fa4cdc7884f8" -Scope Site -ErrorAction SilentlyContinue
38+
39+
# Ensure parent content type is available
40+
$parentContentType = $null
41+
while($null -eq $parentContentType ) {
42+
$parentContentType = Get-PnPContentType -Identity "Document Set"
43+
Start-Sleep -Seconds 5
44+
}
45+
46+
# Create custom Document Set content type
47+
Add-PnPContentType -Name $docsetCTName -ParentContentType $parentContentType -Group "Doc Set Content Types" -ErrorAction SilentlyContinue | Out-Null
48+
49+
# Add columns to content type
50+
$columnsToAddToDocSet.Split(",") | ForEach-Object {
51+
Add-PnPFieldToContentType -Field $_ -ContentType $docsetCTName | Out-Null
52+
}
53+
54+
# Exclude system libraries
55+
$ExcludedLists = @("Access Requests", "App Packages", ... ) # (truncated for brevity)
56+
57+
Get-PnPList | Where-Object { $_.BaseTemplate -eq 101 -and $_.Hidden -eq $False -and $_.Title -notin $ExcludedLists } | ForEach-Object {
58+
$list = Get-PnPList -Identity $_
59+
Set-PnPList -Identity $list -EnableContentTypes $True
60+
Add-PnPContentTypeToList -List $list -ContentType $docsetCTName | Out-Null
61+
Set-PnPDefaultContentTypeToList -List $list -ContentType $docsetCTName
62+
63+
# Create document sets and set metadata
64+
$docSetToAdd.Split(",") | ForEach-Object {
65+
$docSetName = $_
66+
$docSet = Add-PnPDocumentSet -List $list -ContentType $docsetCTName -Name $docSetName
67+
$docSetItem = Get-PnPListItem -List $list -Query "<View><Query><Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='Text'>$docSetName</Value></Eq></Where></Query></View>"
68+
Set-PnPListItem -List $list -Identity $docSetItem.Id -Values @{Company="Company A"; Department="Finance"; } | Out-Null
69+
Write-Host "Document set '$docSetName' created and metadata set."
70+
}
71+
72+
# Add columns to default view
73+
$DefaultListView = Get-PnPView -List $list | Where-Object { $_.DefaultView -eq $True }
74+
$columnsToAddToDocSet.Split(",") | ForEach-Object {
75+
if ($DefaultListView.ViewFields -notcontains $_) {
76+
try {
77+
$DefaultListView.ViewFields.Add($_)
78+
$DefaultListView.Update()
79+
Invoke-PnPQuery
80+
Write-Host -f Green "$_ column added to the Default View in library $($list.Title)!"
81+
} catch {
82+
Write-Host -f Red "Error adding $_ column to the View! $($list.Title)"
83+
}
84+
}
85+
}
86+
}
87+
```
88+
89+
[!INCLUDE [More about PnP PowerShell](../../docfx/includes/MORE-PNPPS.md)]
90+
91+
***
92+
93+
## Source Credit
94+
95+
Sample first appeared on [Automate SharePoint Document Set Configuration with PowerShell](https://reshmeeauckloo.com/posts/powershell-sharepoint-documentset-configuration/)
96+
97+
## Contributors
98+
99+
| Author(s) |
100+
|-----------|
101+
| [Reshmee Auckloo](https://github.com/reshmee011) |
102+
103+
104+
[!INCLUDE [DISCLAIMER](../../docfx/includes/DISCLAIMER.md)]
105+
<img src="https://m365-visitor-stats.azurewebsites.net/script-samples/scripts/spo-multiline-field-properties" aria-hidden="true" />
Loading
Loading
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
[
2+
{
3+
"name": "spo-documentset-configuration",
4+
"source": "pnp",
5+
"title": "Configure SharePoint Document Set",
6+
"shortDescription": "This script streamlines the setup of Document Sets across multiple libraries by enabling the feature, customisting and integrating into document libraries.",
7+
"url": "https://pnp.github.io/script-samples/spo-documentset-configuration/README.html",
8+
"longDescription": [
9+
"This script streamlines the setup of Document Sets across multiple libraries by enabling the feature, creating a custom content type, adding site columns, and integrating it into document libraries while excluding system libraries. It also automates the creation of sample document sets with metadata and enhances the default view by adding relevant columns."
10+
],
11+
"creationDateTime": "2025-05-18",
12+
"updateDateTime": "2025-05-18",
13+
"products": [
14+
"SharePoint",
15+
"Libraries"
16+
],
17+
"metadata": [
18+
{
19+
"key": "PNP-POWERSHELL",
20+
"value": "3.0"
21+
}
22+
],
23+
"categories": [
24+
"SharePoint"
25+
],
26+
"tags": [
27+
"modern",
28+
"Connect-PnPOnline",
29+
"Get-PnPField",
30+
"Set-PnPField",
31+
"Invoke-PnPQuery",
32+
"Get-PnPView",
33+
"Add-PnPDocumentSet",
34+
"Get-PnPListItem",
35+
"Set-PnPListItem",
36+
"Set-PnPDefaultContentTypeToList",
37+
"Add-PnPContentTypeToList",
38+
"Set-PnPList",
39+
"Get-PnPList",
40+
"Add-PnPFieldToContentType",
41+
"Add-PnPContentType",
42+
"Enable-PnPFeature"
43+
],
44+
"thumbnails": [
45+
{
46+
"type": "image",
47+
"order": 100,
48+
"url": "https://raw.githubusercontent.com/pnp/script-samples/main/scripts/spo-documentset-configuration/assets/preview.png",
49+
"alt": ""
50+
}
51+
],
52+
"authors": [
53+
{
54+
"gitHubAccount": "reshmee011",
55+
"company": "",
56+
"pictureUrl": "https://avatars.githubusercontent.com/u/7693852?v=4",
57+
"name": "Reshmee Auckloo"
58+
}
59+
],
60+
"references": [
61+
{
62+
"name": "Want to learn more about PnP PowerShell and the cmdlets",
63+
"description": "Check out the PnP PowerShell site to get started and for the reference to the cmdlets.",
64+
"url": "https://aka.ms/pnp/powershell"
65+
}
66+
]
67+
}
68+
]

0 commit comments

Comments
 (0)