After investigating a bit I have found that there are 2 important fields of an Item. And they are as follows
_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.
_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