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?, Here, 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 

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!"
} 

SharePoint Online: Enable Document ID Service using PowerShell SharePoint Online: Enable Document ID Service using PowerShell Reviewed by Salaudeen Rajack on March 02, 2019 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.