PnP PowerShell: Enabling and Configuring Document ID in SharePoint
PnP PowerShell: Enabling and Configuring Document ID in SharePoint
The Document ID
feature in SharePoint is a powerful tool that assigns a unique ID to files, making it easier to reference and track documents. The script within the post activates and configures the Document ID feature using PnP PowerShell. Additionally, it covers the Set-PnPSiteDocumentIdPrefix
cmdlet, which simplifies the process of setting a custom prefix for Document IDs.
Why Use Document IDs?
Document IDs provide a consistent and unique identifier for documents across a SharePoint site collection. This is particularly useful for:
- Tracking: Easily locate and reference documents, even if they are moved to a different library or folder.
- Auditing: Maintain a reliable record of document usage and changes.
- Automation: Simplify workflows that rely on document references.
Steps to Enable and Configure Document IDs
1. Prerequisites
Before enabling the Document ID feature, ensure the following:
- Install the PnP PowerShell module.
- Sufficient permissions to manage site features, e.g. Site Collection Administrator
2. Enable and Configure the Document ID Feature
The following script enables the Document ID feature for a SharePoint site:
#tenant level settings to allow property bag update
#Set-PnPTenant -AllowWebPropertyBagUpdateWhenDenyAddAndCustomizePagesIsEnabled $true
Set-PnPSite -Identity $SiteUrl -NoScriptSite $false #If you change this setting for a classic team site, it will be overridden by the Custom Script setting in the admin center within 24 hours.
#Configure the Document ID properties
Set-PnPPropertyBagValue -Key "docid_enabled" -Value "1"
Set-PnPPropertyBagValue -Key "docid_msft_hier_siteprefix" -Value $DocIDPrefix
param (
[Parameter(Mandatory = $true)]
[string] $siteUrl,
[Parameter(Mandatory = $true)]
[string] $DocIDPrefix #The Document ID prefix must be 4 to 12 characters long, and contain only digits (0-9) and letters.
)
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
$featureId = "b50e3104-6812-424f-a011-cc90e6327318" #Document Id feature
# Get Feature from SharePoint site
$spFeature = Get-PnPFeature -Scope Site -Identity $featureId
if($null -eq $spFeature.DefinitionId) {
# Activate the site feature
Enable-PnPFeature -Scope Site -Identity $featureId
}
#Set document Id prefix to DocIDPrefix for all existing and new documents in the current site collection. Note that this will take a while (possibly up to 48 hours) to complete as SharePoint Online will need to recalculate and reassign the unique document Id of all files in the site collection
Set-PnPSiteDocumentIdPrefix -DocumentIdPrefix $DocIDPrefix -ScheduleAssignment $true -OverwriteExistingIds $true
$ExcludedLists = @("Access Requests", "App Packages", "appdata", "appfiles", "Apps in Testing", "Cache Profiles", "Composed Looks", "Content and Structure Reports", "Content type publishing error log", "Converted Forms",
"Device Channels", "Form Templates", "fpdatasources", "Get started with Apps for Office and SharePoint", "List Template Gallery", "Long Running Operation Status", "Maintenance Log Library", "Images", "site collection images"
, "Master Docs", "Master Page Gallery", "MicroFeed", "NintexFormXml", "Quick Deploy Items", "Relationships List", "Reusable Content", "Reporting Metadata", "Reporting Templates", "Search Config List", "Site Assets", "Preservation Hold Library",
"Site Pages", "Solution Gallery", "Style Library", "Suggested Content Browser Locations", "Theme Gallery", "TaxonomyHiddenList", "User Information List", "Web Part Gallery", "wfpub", "wfsvc", "Workflow History", "Workflow Tasks", "Pages")
Get-PnPList | Where-Object { $_.BaseTemplate -eq 101 -and $_.Hidden -eq $False -and $_.Title -notin $ExcludedLists} | foreach {
#Get the Default View from the list
$DefaultListView = Get-PnPView -List $_ | Where {$_.DefaultView -eq $True}
#Add column to the View
If($DefaultListView.ViewFields -notcontains "_dlc_DocIdUrl")
{
try {
$DefaultListView.ViewFields.Add("_dlc_DocIdUrl")
$DefaultListView.Update()
Invoke-PnPQuery
Write-host -f Green "Document ID column Added to the Default View in library $($_.Title)!"
}
catch {
Write-host -f Red "Error Adding Document ID column to the View! $($_.Title)"
}
}
else
{
Write-host -f Yellow "Document ID column already exists in the View! $($_.Title)"
}
}
The script outcome
- Enable Document ID Feature
- Configure Document ID Prefix
- Add the Document ID column to library Views
Navigating to the site, all libraries will have the Document ID column in the default view.