Create a cross-site lookup field using PowerShell

Sometimes you need to create a lookup field to look up a list in another site (not the parent site).

This is how  you can do it using PowerShell:

On SharePoint server, run PowerShell as admin:


Add-PsSnapin Microsoft.SharePoint.PowerShell
$targetWeb = Get-SPWeb http://xxxxxx/Subsite1
$sourceWeb = Get-SPWeb http://xxxxxx/Subsite2
$targetList = $targetWeb.Lists.item("The target list name")
$sourceList = $sourceWeb.Lists.item("The source list name")
$NameTheLookupFieldToBeCreated = "Name of the lookup field to be created"
$ColumnInSourceList = "Name of the column in the source list"
### No need to change after this line ###
$targetList.Fields.AddLookup($NameTheLookupFieldToBeCreated, $sourceList.id, "false")
$LookupField = $targetList.Fields[$NameTheLookupFieldToBeCreated]
$LookupField.LookupWebId = $sourceList.ParentWeb.ID
$LookupField.LookupField = $sourceList.Fields[$ColumnInSourceList].InternalName
$LookupField.Update();

Advertisements
This entry was posted in PowerShell, SharePoint 2010. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s