Create Lookup Site Column from Subsite using PowerShell

Creating lookup field as site column was explained in my another 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 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 list on another site.

PowerShell script 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

#Where the Lookup Site column Going to get created
$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
    #Add Lookup Field
    $ChildLookupColumn = $ChildWeb.Fields.AddLookup($ChildLookupColumnName,$,$False)
    $ChildLookupColumn = $ChildWeb.Fields[$ChildLookupColumnName]
    #Setup lookup Field property
    $ChildLookupColumn.LookupWebId = $ParentWeb.ID
    $ChildLookupColumn.LookupField = $ParentList.Fields[$ParentLookupColumnName].InternalName
    write-host "Lookup field added successfully!" -f green
    write-host "Field Exists already!" -f red
This PowerShell script creates SharePoint lookup column from list on another site! BTW, Site columns must be created on Root web, so that it can be consumed by any subsite underneath.

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.