Question

Once we activate the Document ID Service from the Site Collection Features.

Whenever a document is uploaded in a library a Unique Document ID is generated. And this ID looks something like PNR6U3ASXZE2-1206519163-68. Which is a click able link and gets you to the document, irrespective whether document lies in same library or moved to some other library in future.

I would want to get this very useful ID and Link using CSOM PowerShell and store in a different library which contains the mapping partner documents of the original.

Simple Question: How to get the Doc ID and URL of a Document in library generated by Doc ID service using CSOM PowerShell?

Was it helpful?

Solution

After investigating a bit I have found that there are 2 important fields of an Item. And they are as follows

  1. _dlc_DocId: It contains simple text value such as PNR6U3ASXZE2-1206519163-68. Which may be useful if only we need text, but not when we need the URL too.
  2. _dlc_DocIdUrl: This is the one in which we all should be interested, because it gives both the ID and URL. See the code below.

Note: I have done this in SharePoint Online, But this can be easily done in SP2013 On premise too.

First connect to SharePoint Site.

$ctx = New-Object Microsoft.SharePoint.Client.ClientContext("https://somesite.sharepoint.com")
$web = $ctx.Web


# Authenticating
#
$credentials = Get-Credential
$pass =  ConvertTo-SecureString $credentials.Password -AsPlainText -Force
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($credentials.UserName, $pass)

# Load the object
#
$ctx.Load($web)
$ctx.ExecuteQuery()

Get the file object and its field Items.

$ObjFile = $web.GetFileByServerRelativeUrl("/DocLibName/File.docx")
$item = $ObjFile.ListItemAllFields
$ctx.Load($ObjFile)
$ctx.Load($item)
$ctx.ExecuteQuery()

Once we get the files item fields, we can store them in values and use anywhere we need.

# $item["_dlc_DocIdUrl"].Url property gives the url to the document
#
$Url = $item["_dlc_DocIdUrl"].Url

# $item["_dlc_DocIdUrl"].Description gives nothing but the unique Id, i.e. PNR6U3ASXZE2-1206519163-68
#
$Description = $item["_dlc_DocIdUrl"].Description
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top