SharePoint Online: Enable Document ID Service using PowerShell

Document ID Service feature automatically assigns a unique ID to documents uploaded which helps to track and manage documents easily. Configuring document ID service is explained in my other post How to Configure Document ID Service Feature in SharePoint Online?
SharePoint Online Enable Document ID Service using PowerShell

Let's see how to enable document ID service using PowerShell:

Step 1: Activate Document ID Feature in SharePoint Online
As a first step to enable document ID, we need to activate a feature in the site collection.
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Sales"
$FeatureId = "b50e3104-6812-424f-a011-cc90e6327318"
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)
 
#Get the Feature
$Feature = Get-PnPFeature -Scope Site -Identity $FeatureId
 
#Get the Feature status
If($Feature.DefinitionId -eq $null)
{    
    #enable document id feature in sharepoint online using powershell
    Write-host -f Yellow "Activating Document ID Service Feature..."
    Enable-PnPFeature -Scope Site -Identity $FeatureId -Force
 
    Write-host -f Green "Document ID Service Feature Activated Successfully!"
}
Else
{
    Write-host -f Yellow "Document ID Service Feature is already active!"
} 

Step 2: Configure SharePoint Online Document ID Settings
Once the feature is enabled, we need to configure it at the site collection level. Before configuring document ID settings, you must enable custom scripting for the SharePoint Online site collection.
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Sales"
$DocIDPrefix = "Sales" 
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Configure the Document ID properties
Set-PnPPropertyBagValue -Key "docid_enabled" -Value "1"
Set-PnPPropertyBagValue -Key "docid_msft_hier_siteprefix" -Value $DocIDPrefix 

Tips: You can use the above PowerShell script to reset document ID prefix! Once you set the document ID prefix with PowerShell, make sure you go to: Site settings >> Document ID settings >> Choose "Reset all Document IDs in this Site Collection to begin with these characters" manually!


Step 3: Add Document ID Column to SharePoint Online 
Wait for some time, and you can see the document ID column populated from the configuration specified. If you need, you can add that column to the default view of your document libraries.
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Sales"
$LibraryName = "Team Documents" 

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get the Default View from the list
$DefaultListView  =  Get-PnPView -List $LibraryName | Where {$_.DefaultView -eq $True}

#Add column to the View
If($DefaultListView.ViewFields -notcontains "_dlc_DocIdUrl")
{
    $DefaultListView.ViewFields.Add("_dlc_DocIdUrl")
    $DefaultListView.Update()
    Invoke-PnPQuery

    Write-host -f Green "Document ID column Added to the Default View!"
}
else
{
    Write-host -f Yellow "Document ID column already exists in the list!"
} 

2 comments:

  1. Do you know the property for the checkbox of reset document id in sharepoint site collection like docid_enabled?

    ReplyDelete
    Replies
    1. Well, it's not possible to reset the Document ID prefix through PowerShell in SharePoint online (AFAIK).

      Go to site settings >> Document ID settings >> Choose "Reset all Document IDs in this Site Collection to begin with these characters" manually.

      Delete

Please Login and comment to get your questions answered!

Powered by Blogger.