{"id":1825,"date":"2013-05-06T04:58:55","date_gmt":"2013-05-06T04:58:55","guid":{"rendered":"https:\/\/poiseddevelopers.com\/reality-tech\/?p=1825"},"modified":"2024-04-26T12:58:46","modified_gmt":"2024-04-26T12:58:46","slug":"programatically-reassigning-content-types-in-a-library","status":"publish","type":"post","link":"https:\/\/poiseddevelopers.com\/reality-tech\/programatically-reassigning-content-types-in-a-library\/","title":{"rendered":"Programatically reassigning Content Types in a library"},"content":{"rendered":"<h2>Reassigning Content Types in a library using PowerShell<\/h2>\n<p>It is useful to be able to change the Content Type for each document in a library to a new Content Type. Perhaps you are consolidating, or just moving to a new Content Type hierarchy. There are a few tricks to changing a Content Type for a document. First, you\u2019ll need to force a check-in if a document is checked out. Next, you\u2019ll want to reference the Content Type by ID, specifically using this method: $list.Items.GetItemById($item.ID). Any other attempt to reassign a Content Type will fail. Lastly, either do a systemupdate() or clean up after yourself by restoring the timestamp, editor, and delete interim versions. Note there needs to be sufficient metadata, or the change will result in an error and\/or the document being left checked out. This function traps and reports this condition. Here\u2019s the function we will use:<\/p>\n<pre lang=\"php\">\r\nfunction Reset-SPFileContentType ($WebUrl, $ListName, $OldCTName, $NewCTName)\r\n{\r\n    #Get web, list and content type objects\r\n    $web = Get-SPWeb $WebUrl\r\n    $list = $web.Lists[$ListName]\r\n    $oldCT = $list.ContentTypes[$OldCTName]\r\n    $newCT = $list.ContentTypes[$NewCTName]\r\n    $newCTID = $newCT.ID\r\n     \r\n    #Check if the values specified for the content types actually exist on the list\r\n    if (($oldCT -ne $null) -and ($newCT -ne $null))\r\n    {\r\n        #Go through each item in the list\r\n        $list.Items | ForEach-Object {\r\n            #Check if the item content type currently equals the old content type specified\r\n            if ($_.ContentType.Name -eq $oldCT.Name)\r\n            {\r\n                $ForcedCheckin=$false;\r\n                if ($_.File.CheckOutType -ne \"None\")\r\n                {\r\n                   try { \r\n                     $_.File.CheckIn(\u201cChecked In By Administrator\u201d);\r\n                      \r\n                     write-host -foregroup Yellow \"Forced checkin for $($_.File.Title)\"\r\n                   }\r\n                   catch { \r\n                      \r\n                     write-host -foregroup Red \"FAILED to Force checkin for $($_.File.Title)\"\r\n                     $ForcedCheckin=$false;\r\n                   }\r\n                }\r\n \r\n                #Check the check out status of the file\r\n                if ($_.File.CheckOutType -eq \"None\")\r\n                {\r\n \r\n                   $item=$_\r\n                   [System.DateTime]$date = $item[\"Modified\"]\r\n                   $user = New-Object microsoft.SharePoint.SPFieldUserValue($web, $item[\"Editor\"])\r\n \r\n                   try { #untested try, failure could be due to inadequate required metadata\r\n                    #Change the content type association for the item\r\n      [Microsoft.SharePoint.SPSecurity]::RunWithElevatedPrivileges(\r\n      {\r\n        $item2 = $list.Items.GetItemById($item.ID);\r\n#CHECKOUT NOT REALLY NEEDED, IF MUSTCHECKOUT IS DISABLED EARLIER\r\n#        $item2.File.CheckOut()\r\n \r\n#write-host $item2['Name']\r\nWrite-Host \".\" -NoNewline\r\n \r\n         \r\n        $item2[\"ContentTypeId\"] = $newCTID\r\n        $item2.Update()\r\n \r\n        $item2[\"Modified\"] = $date;\r\n        $item2[\"Editor\"] = $user;\r\n        $item2.Update()\r\n \r\n        #get rid of the last two versions now, trapping any errors \r\n        try { $item2.Versions[1].delete() } catch {write-host -foregroundcolor red \"Error (1) could not delete old version of $($item2['Name'])\"}\r\n        try { $item2.Versions[1].delete() } catch {write-host -foregroundcolor red \"Error (2) could not delete old version of $($item2['Name'])\"}\r\n        if ($ForcedCheckin)\r\n        {try { $item2.Versions[1].delete() } catch {write-host -foregroundcolor red \"Error (3) could not delete ForcedCheckin version of $($item2['Name'])\"}}\r\n \r\n#get rid of the last version now\r\n \r\n#        $item2.File.CheckIn(\"Content type changed to \" + $newCT.Name, 1)\r\n     } )\r\n \r\n    } catch { write-host -foregroundcolor red \"Error (possibly inadequate metadata) updating file: $($_.Name) from $oldCT.Name to $($newCT.Name)\"\r\n               \r\n             }\r\n \r\n            }\r\n                else\r\n                {\r\n                    write-host -foregroundcolor red \"File $($_.Name) is checked out to $($_.File.CheckedOutByUser.ToString()) and cannot be modified\";\r\n                    \r\n                }\r\n            }\r\n            else\r\n            {\r\n                 #Write-Host -ForegroundColor DarkRed \"File $($_.Name) is associated with the content type $($_.ContentType.Name) and shall not be modified `n\";\r\n            }\r\n        }\r\n    }\r\n    else\r\n    {\r\n        write-host -foregroundcolor red \"One of the content types specified has not been attached to the list $($list.Title)\"\r\n         \r\n    }\r\n \r\n  $web.Dispose()\r\n}\r\n<\/pre>\n<p>Next, let\u2019s just call the function passing in the old name and the new name:<\/p>\n<pre lang=\"php\">\r\nreset-spfilecontenttype -weburl $weburl -listname $JPlib  -oldctname \"Documents\" -newctname \"DOCS\"\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Reassigning Content Types in a library using PowerShell It is useful to be able to change the Content Type for each document in a library to a new Content Type. Perhaps you are consolidating, or just moving to a new Content Type hierarchy. There are a few tricks to changing a Content Type for a [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":1826,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[39],"tags":[],"class_list":["post-1825","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-content-types"],"acf":[],"_links":{"self":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts\/1825","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=1825"}],"version-history":[{"count":1,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts\/1825\/revisions"}],"predecessor-version":[{"id":1831,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts\/1825\/revisions\/1831"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/media\/1826"}],"wp:attachment":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/media?parent=1825"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/categories?post=1825"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/tags?post=1825"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}