|
| 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" /> |
0 commit comments