Can we create a cross site lookup field?
Yes of course, it can be. Thought it is not possible OTB, a simple trick will do this.
Scenario: Creating a cross site lookup field between the two peer web sites or sub web sites under a same site collection.
Example: Create a lookup field between two sites http:/sitecollection/webA & http://sitecollection/webB
The trick is just to set the lookup field webId with the target list webId.
Code snippet to create cross site lookup field programatically:
Yes of course, it can be. Thought it is not possible OTB, a simple trick will do this.
Scenario: Creating a cross site lookup field between the two peer web sites or sub web sites under a same site collection.
Example: Create a lookup field between two sites http:/sitecollection/webA & http://sitecollection/webB
The trick is just to set the lookup field webId with the target list webId.
Code snippet to create cross site lookup field programatically:
private bool CreateCrossSiteLookupField(SPWeb sourceWeb, SPWeb targetWeb, string sourceListTitle, string targetListtitle, string lookUpFieldName, string lookupValueColumn, bool isRequiredField)
{
////Get the source and target lists
SPList sourceList = sourceWeb.Lists.TryGetList(sourceListTitle);
SPList targetList = targetWeb.Lists.TryGetList(targetListtitle);
if (null != sourceList && null != targetList)
{
////Add a lookup field to source list with target list as lookup list
sourceList.Fields.AddLookup(lookUpFieldName, targetList.ID, isRequiredField);
////Get the created lookup field from source list
SPFieldLookup lookupField = (SPFieldLookup)sourceList.Fields[lookUpFieldName];
////Set the lookup field's webID with target list webId
lookupField.LookupWebId = targetList.ParentWeb.ID;
////Set the lookup field's display field
lookupField.LookupField = targetList.Fields[lookupValueColumn].InternalName;
////Finally update the lookup field
lookupField.Update();
}
return true;
}
Code snippet to create cross site lookup field with PowerShell:Add-PSSnapIn "Microsoft.SharePoint.Powershell"
#Get the webs and lists
$webA = Get-SPWeb http://siteurl/webA/
$webB = Get-SPWeb http://siteurl/webB/
$sourceList = $webA.Lists.item("SourceList")
$targetList = $webB.Lists.item("TargetList")
#Add a lookup field to source list with target list as lookup list
$sourceList.fields.AddLookup("LookupFieldName", $targetList.id, "true")
#Get the created lookup field from source list
$lookupField = $sourceList.Fields["LookupFieldName"]
#Set the lookup field's webID with target list webId
$lookupField.LookupWebId = $targetList.ParentWeb.ID
#Set the lookup field's display field
$lookupField.LookupField = $targetList.Fields["Title"].InternalName
$lookupField.Update();