Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imagina/b7d3393e04a175472f565cb82e4da5ca to your computer and use it in GitHub Desktop.
Save imagina/b7d3393e04a175472f565cb82e4da5ca to your computer and use it in GitHub Desktop.
Convert Sharepoint Choice Column to Lookup Column - PowerShell
#Install PnP PowerShell in your local machine https://github.com/pnp/powershell
$SiteUrl = "https://ssite.sharepoint.com/sites/SPSiteName";
$ListMainName = "List Name";
$ListCompaniesName = "Lookup List Name"
# Login using Web
Connect-PnPOnline -Url $SiteUrl -UseWebLogin
#If we can't use PnP
#$context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl);
#$context = Get-PnPContext
# Retrieve the lists
$ListCompanies = Get-PnPListItem -List $ListCompaniesName -ErrorAction Stop
#If we require to limit the fields for a big List
#$fields = "Id","Title","Company","CompanyTmp";
$ListMain = Get-PnPListItem -List $ListMainName -ErrorAction Stop
# Enumerate the list and update column
$ItemCount = $ListMain.Count;
"`Main List contains $itemCount items ...`n"
$i = 1;
ForEach ($Item in $ListMain) {
#$Item["CompanyTmp"]
#if( ($Item["CompanyTmp"]) -and ($Item["Company"] -eq $Item["CompanyTmp"]["LookupValue"])) {
# break;
#}
#$Item.FieldValues;
# Match choice to lookup
ForEach ($ItemLU in $ListCompanies) {
$Item["CompanyName"]
If ($ItemLU["Title"] -eq $Item["CompanyName"]) {
"Updating item: $i of $ItemCount"
Set-PnPListItem -List $ListMainName -Identity $Item["ID"] -Values @{"CompanyTmp" = $ItemLU["ID"]}
}
}
$i++
#Break for testing
#if($i -eq 4) {
# "end"
# break;
#}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment