Quantcast
Channel: SharePoint Diary
Viewing all articles
Browse latest Browse all 1058

Create Lookup Site Column from Subsite using PowerShell

$
0
0
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
$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 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.

Viewing all articles
Browse latest Browse all 1058

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>