Convert Choice Column to Lookup Column

By James|04/26/2013|,

Another day, another challenge!  I was looking at a document library with 1000+ files where a "Document Type" column had been added as a choice.  Unfortunately, the column was supposed to be a lookup.  I created a new lookup column and pointed it to the correct list.  That's great, but editing the properties of all 1000+ files just didn't sound like good times to me.  The following script walks the document library, pulls each "Document Type" choice, matches it against the lookup list and updates the new "Document Type" column.

Add-PSSnapin Microsoft.SharePoint.PowerShell -EA SilentlyContinue
# What are we going to look at?
$myWeb = "http://some_portal"
$myList = "Some Library"

# Master document type lookup list
$myListLU = "Lookup List"

# Retrieve the lists
$web = Get-SPWeb $myWeb
$list = $web.GetList("$myWeb\$myList")
$listLU = $web.GetList("$myWeb\lists\$myListLU")

# Enumerate the list and update column
$itemCount = $list.Items.Count
"`nDocument library contains $itemCount items ...`n"
$i = 1
ForEach ($item in $list.Items) {
  "Updating item: $i of $itemCount"
  $oldData = $item["Document Type"]

  # Match choice to lookup
  ForEach ($itemLU in $listlU.Items) {
    If ($itemLU["Title"] -eq $oldData) {
      $NewLookupFieldValue = New-Object Microsoft.Sharepoint.SPFieldLookupValue $itemLU["ID"], $itemLU["Title"]
      $item["DocTypeTemp"] = $NewLookupFieldValue
      $item.Update()
    }
  }
  $i++
}
$web.Dispose()

 

Copyright 2011 - 2024 The Lazy IT Admin | All Rights Reserved
menu-circlecross-circle linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram