Create Lookup on List in another Web

Here is a simple powershell script, that creates Lookup column on targetList from sourceList.


$targetWeb = Get-SPWeb http://web.domain.local/site/webroot/web1/
$targetList = $targetWeb.Lists.TryGetList("Target List Name")

$sourceWeb = Get-SPWeb http://web.domain.local/site/webroot/anotherweb/
$sourceList = $sourceWeb.Lists.TryGetList("Source List Name")

$targetList.Fields.AddLookup("NewLookup", $sourceList.id, $false)
$LookupField = $targetList.Fields["NewLookup"]
$LookupField.Required = $false
$LookupField.LookupWebId = $sourceList.ParentWeb.ID
$LookupField.LookupField = $sourceList.Fields.GetField("FieldNameToDisplay").InternalName
$LookupField.Update();

Marek

Currently working as SharePoint Developer combining both back-end & front-end development scenarios. Also enthusiastic about Office 365 & Azure solutions.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *