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();

Leave a Comment