SharePoint Online: Fix Lookup Field using PowerShell
Problem: SharePoint Online lookup column in a list is broken when the parent lookup list is recreated!
Root Cause:
SharePoint Online list lookup columns are connected with its parent lookup list column through the “List” (GUID of the parent list) and “ShowField” attributes. When the parent lookup list is re-created, its GUID is regenerated, and the connectivity between parent and child lookup lists is broken, and lookup field values become empty!
How to Fix Broken Lookup Column in SharePoint Online?
If you come across the above scenario and your lookup column is broken with empty values, you have to re-link the lookup column with its parent list. Use this PowerShell script to fix the lookup columns in the SharePoint Online list.
#Parameters
$SiteURL = "https://Crescent.sharepoint.com/sites/pmo"
$ParentListName = "Projects"
$ChildListName = "ProjectMilestones"
#Field display names - Case sensitive!
$ParentFieldTitle = "Title"
$ChildFieldTitle = "Project"
#Connect to SharePoint Online
Connect-PnPOnline $SiteURL -Credentials (Get-Credential)
#Get Parent List & Child field
$ParentList = Get-PnPList -Identity $ParentListName
$ChildField = Get-PnPField -List $ChildListName -Identity $ChildFieldTitle
#Get the Field Schema XML
[Xml]$Schema = $ChildField.SchemaXml
#Update Field Schema with New Parent List and Field
$Schema.Field.Attributes["List"].'#text' = "{$($ParentList.Id.Guid)}"
$Schema.field.Attributes["ShowField"].'#text' = $ParentFieldTitle
$ChildField.SchemaXml = $Schema.OuterXml
$ChildField.UpdateAndPushChanges($true)
Invoke-PnPQuery
Also, Remember, We have the parent lookup list saved as a list template with content and created a new list from it so that the associated row values remain the same! Because Field value in each row of the lookup field of the child lookup list will be pointing to a row ID in the parent lookup list.
Hi, Thank you for the posting!
Im getting the error below,
The property ‘#text’ cannot be found on this object. Verify that the property exists and can be set.
At line:20 char:48
+ $Schema.Field.Attributes[“List”].’#text’ = “{$($ParentList.Id.Guid)}”
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound
The property ‘#text’ cannot be found on this object. Verify that the property exists and can be set.
At line:21 char:1
+ $Schema.field.Attributes[“ShowField”].’#text’ = $ParentFieldTitle
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound
Hi Vagsfyr,
Confirmed the above script works just fine and fixes the broken lookup list. Make sure the field you are fixing is a child lookup field in the list.
Thank you for the posting. I ran your scripts and “Get information from” is still blank. {$($ParentList.Id.Guid)} got the correct Guid and $Schema.field.Attributes[“ShowField”].’#text’ = $ParentFieldTitle got the field name. Can you please let me know what did I miss?
Field Schema XML is case-sensitive! Give it a check.
This has been a thorn in my side FOREVER! It works great (If you fix the $ChildListName variable in row 14)!
Fixed the Typo! Thanks.
Worked like a charm, thank you very much for this helpful tutorial!