Create Lookup Site Column from Subsite using PowerShell

Creating a lookup field as a site column was explained in my other post: Create a Cross-Site Lookup Site Column in SharePoint 2013. Now got a requirement to create a lookup site column from the subsite’s list! Unfortunately, there is no UI to create a site column from subsite list values. Lookup site columns can be created only from the lists of the same site.

PowerShell comes to the rescue! We can create a lookup site column referencing any site of the site collection using PowerShell! Here is the PowerShell script to create a lookup column from the list on another site.

PowerShell to create lookup site column from subsite’s (or different site) list:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue 

#Where the Source List for Lookup Exists
$ParentWebURL="https://portal.crescent.com/sales/"
$ParentListName="Regions"
$ParentLookupColumnName="Region"

#Where the Lookup Site column Going to get created
$ChildWebURL="https://portal.crescent.com"
$ChildLookupColumnName="Sales Region"
 
#Get the Parent and Child Webs and List
$ParentWeb = Get-SPWeb $ParentWebURL
$ParentList = $ParentWeb.Lists[$ParentListName]
$ChildWeb = Get-SPWeb $ChildWebURL

#Check if Field exists already
if(!$ChildWeb.Fields.ContainsField($ChildLookupColumnName))
{
    #Add Lookup Field
    $ChildLookupColumn = $ChildWeb.Fields.AddLookup($ChildLookupColumnName,$ParentList.id,$False)
    $ChildLookupColumn = $ChildWeb.Fields[$ChildLookupColumnName]
 
    #Setup lookup Field property
    $ChildLookupColumn.LookupWebId = $ParentWeb.ID
    $ChildLookupColumn.LookupField = $ParentList.Fields[$ParentLookupColumnName].InternalName
    #$ChildLookupColumn.AllowMultipleValues=$true
    $ChildLookupColumn.update()
    write-host "Lookup field added successfully!" -f green
}
else
{
    write-host "Field Exists already!" -f red
} 

This PowerShell script creates a SharePoint lookup column from the list on another site! BTW, Site columns must be created on the Root web, so that it can be consumed by any subsite underneath.

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

Leave a Reply