{"id":572,"date":"2013-03-22T11:20:02","date_gmt":"2013-03-22T11:20:02","guid":{"rendered":"https:\/\/poiseddevelopers.com\/reality-tech\/?p=572"},"modified":"2024-04-26T12:55:14","modified_gmt":"2024-04-26T12:55:14","slug":"propagating-legacy-document-ids-into-sharepoint","status":"publish","type":"post","link":"https:\/\/poiseddevelopers.com\/reality-tech\/propagating-legacy-document-ids-into-sharepoint\/","title":{"rendered":"Propagating legacy Document IDs into SharePoint"},"content":{"rendered":"<h4>Document IDs in SharePoint<\/h4>\n<p>Document IDs are generated automatically by SharePoint when configured, but what if you have your own legacy Document IDs that you want to use? There is a way to push them in.<\/p>\n<p>The first trick is recognizing there are two internal fields created when Document IDs are enabled.<br role=\"presentation\" data-uw-rm-sr=\"\" \/>ows__dlc_DocId : The actual Document ID<br role=\"presentation\" data-uw-rm-sr=\"\" \/>ows__dlc_DocIdUrl : The special Redir.ASPX based URL with the document ID within it<\/p>\n<p>Let\u2019s examine the later field in a bit more detail. This contains a URL with a reference to \u201cDocIdRedir.aspx\u201d which is an ASPX ghosted page that will redirect either via local service lookup, or a fallback to a search based lookup. Here\u2019s what it looks like:<\/p>\n<p>http :\/\/server\/_layouts\/DocIdRedir.aspx?ID=DOC-1000-3109, DOC-1000-3109\u2033<\/p>\n<p>Note the comma, then the repeated Document ID.<\/p>\n<p>Now imagine if there\u2019s a field in our document library that has the legacy Document ID we want to use.<\/p>\n<p>The script below goes through, and systematically replaces the existign Document ID with the Legacy one we want to use.<\/p>\n<pre lang=\"php\">[system.reflection.assembly]::LoadWithPartialName(\"Microsoft.SharePoint\") \r\nWrite-Host (\"$(get-date) Running script to assing DOcIDs\")\r\n$SiteFilter = \"*\"\r\n$siteUrl = \"http :\/\/SharePoint\" #use your own Web App URL\r\n \r\n$ThisLibOnly = $null; #allows for filtered selection of libraries\r\n      \r\nWrite-Host (\"$(get-date) Assigning DocIDs from Legacy IDs\")\r\n \r\n$webApp=Get-SPWebApplication $SiteURL\r\n$Sites = $webApp | Get-SPSite -Limit all\r\nforeach ($site in $Sites)\r\n{\r\n$OutputSuffix=$loopLetter;\r\n$mylogfile=\"C:Tempoutput.log\"\r\n  write-host $site.Url\r\n  \r\n   Write-Host -foregroundcolor darkblue \"$($site.id) - $($site.Url) - $($site.contentdatabase.id) - $($site.contentdatabase.name)\"  \r\n    \r\n   $WebScope = Start-SPAssignment\r\n     \r\n   foreach ( $TargetWeb in $site.AllWebs)\r\n   {\r\n \r\n        Write-Host(\"Working in site: \" + $TargetWeb)\r\n        $ListOfLists = @();\r\n# Loop through all doc libs\r\n        $lists=$TargetWeb.lists;\r\n        $listsCount=$lists.count\r\n     \r\n    for ($ii=0; $ii -lt $listsCount; $ii++)\r\n    {\r\n    $JPlib=$lists[$ii];\r\n       if ( ($JPlib.BaseType -ne \"DocumentLibrary\") -or ($JPlib.hidden) )\r\n        {\r\n          # forget the rest and return to top\r\n          Write-Host -foregroundcolor darkred \"fast test skipping Library: $($JPlib)\";   \r\n        }\r\n        elseif ($JPLib.Title -Match \"Photo|Image|SitesAssets|CustomizedsReports|Templates|Pages|Picture|cache|style\")\r\n        {\r\n          # forget the rest and return to top\r\n          Write-Host -foregroundcolor darkred \"fast test skipping Library because it mentions $Matches: $($JPlib)\";   \r\n        }\r\n        elseif (!$JPlib.contenttypesenabled)\r\n        {\r\n \r\n        continue; # no content types, no interest\r\n        }\r\n        else\r\n        {  \r\n          Write-Host -foregroundcolor green \"Processing Library: $($JPlib)\";   \r\n          $ListOfLists += $JPlib.title;\r\n        }\r\n    }\r\n     \r\n    foreach ($CurrentLib in $ListofLists)\r\n    {\r\n    $JPlib=$TargetWeb.lists[$CurrentLib];\r\n    $JPlib.title\r\n    if ($JPlib -eq $null)\r\n    {\r\n        Write-Host \"COULD NOT GET LIB $($CurrentLib)\"\r\n        continue;\r\n    }\r\n \r\n          Write-Host -foregroundcolor green \"Processing Library: $($JPlib) in $($TargetWeb)\";   \r\n     \r\n    if (($ThisLibOnly -eq $null) -or\r\n      ($JPlib.title -eq $ThisLibOnly)) \r\n    {\r\n    $JPItems=$JPlib.Items;\r\n    $JPCount=$JPItems.get_count();\r\n     \r\n    if ($JPCount -eq 0) {continue} #can't do much on a library with no items!\r\n          \r\n    for ($i=0; $i -lt $JPCount; $i++)  #Do not use ItemCount, that one includes folders!\r\n    {   \r\n        $JPItem=$JPItems[$i];\r\n        $SourceValue=$JPItem[\"LegacyDocID\"];\r\n         \r\n        if (($SourceValue -eq $null) -or ($SourceValue.length -le 0))\r\n        {\r\n             \r\n            write-host \"-\" -nonewline\r\n            continue; #nothing to assign\r\n        }\r\n        elseif ($JPItem[\"ows__dlc_DocId\"] -ne $SourceValue) #avoid reassigning same value\r\n        {\r\n        Write-Host \"Old DocID=$($JPItem['ows__dlc_DocId']),LegacyDocID=$($SourceValue)\"\r\n        $oldDocIDValue=$JPItem[\"ows__dlc_DocId\"]\r\n        $JPItem[\"ows__dlc_DocId\"] = $SourceValue;\r\n        if ($JPItem[\"ows__dlc_DocIdUrl\"].length -gt 1)\r\n        {\r\n            $JPItem[\"ows__dlc_DocIdUrl\"]= $JPItem[\"ows__dlc_DocIdUrl\"].replace($oldDocIDValue,$SourceValue);\r\n        }\r\n         \r\n        $JPItem.systemupdate() #without generating version\r\n        Write-Host \"$($i): $($JPItem.url)\"\r\n        }\r\n        else\r\n        {  #special!  $JPItem[\"ows__dlc_DocIdUrl\"]=$null;\r\n        Write-Host \"DOcID Match! $($JPItem.url)\"\r\n        Write-Host \"Old DocID=$($JPItem['ows__dlc_DocId']),LegacyDocID=$($SourceValue)\"\r\n        }\r\n             }      \r\n    }\r\n \r\nWrite-Host \"+\" -NoNewline\r\n \r\n     \r\n}# Lib loop\r\n     \r\n$JPlib.Update();\r\n$JPlib=$null;\r\n \r\n} #all libs?\r\n    try\r\n    {\r\n    $TargetWeb.dispose()\r\n    } catch{}\r\n    $TargetWeb=$null;\r\n    Stop-SPAssignment $WebScope\r\n} #all webs?\r\n \r\n \r\n} # all sites?\r\n$site.Dispose()\r\n$site=$null;\r\nStop-SPAssignment $SiteScope\r\nWrite-Host \"SCRIPT COMPLETE $(get-date)\"\r\nShare this entry\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Document IDs in SharePoint Document IDs are generated automatically by SharePoint when configured, but what if you have your own legacy Document IDs that you want to use? There is a way to push them in. The first trick is recognizing there are two internal fields created when Document IDs are enabled.ows__dlc_DocId : The actual [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":575,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[29],"tags":[],"class_list":["post-572","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-document-ids"],"acf":[],"_links":{"self":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts\/572","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/comments?post=572"}],"version-history":[{"count":2,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts\/572\/revisions"}],"predecessor-version":[{"id":3046,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts\/572\/revisions\/3046"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/media\/575"}],"wp:attachment":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/media?parent=572"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/categories?post=572"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/tags?post=572"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}