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 re-generated and the connectivity between parent and child lookup lists is broken and lookup field values become empty!

sharepoint online fix lookup field using powershell

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://crescentintranet.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 $ChildList -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.

Salaudeen Rajack

Information Technology Professional with Two decades of SharePoint Experience.

2 thoughts on “SharePoint Online: Fix Lookup Field using PowerShell

  • May 14, 2021 at 11:27 PM

    This has been a thorn in my side FOREVER! It works great (If you fix the $ChildListName variable in row 14)!

    Reply
  • July 29, 2020 at 8:29 AM

    Worked like a charm, thank you very much for this helpful tutorial!

    Reply

Leave a Reply