Question

I've read a numbered time that SPFieldLookup is cross-site by its nature. But now I'm a little bit confused, here is my SharePoint structure:

  • /SiteCollection
    • /Site1
      • Projects
    • /Site2
      • Customers

Does cross-site mean that I can do a lookup field for Customers and access it within my Projectsform?

And if it is possible, how should I do it? I've looked into those articles but it seem to be for a site and his subsites only:

http://developing-dummy.blogspot.fr/2012/02/sharepoint-and-cross-site-lookup-fields.html

http://tjendarta.wordpress.com/tag/spfieldlookup/

Was it helpful?

Solution

Yes, you're able to create a Lookup column from another site. You can use the script bellow in PowerShell. It'll create a Customer lookup column in your Project list. Before running the script you'll need to update the url and list names with yours.

#Get the webs and lists
$web1 = Get-SPWeb http://sitecollection/site1/
$web2 = Get-SPWeb http://sitecollection/site2/
$projectList = $web1.Lists.item("Projects")
$customerList = $web2.Lists.item("Customers")

#Add a lookup field
$projectList.fields.AddLookup("Customer", $customerList.id, "false")

#Get the created lookup field
$LookupField = $projectList.Fields["Customer"]

#Set the lookup web ID and lookup field
$LookupField.LookupWebId = $customerList.ParentWeb.ID
$LookupField.LookupField = $customerList.Fields["Title"].InternalName
$LookupField.Update();

OTHER TIPS

Via UI it is not possible to create a site-cross lookup column. Taking a look on the Microsoft Documentation there is the method AddLookup(String, Guid, Guid, Boolean) where it is possible to specify also a web.

Example:

Add-PSSnapin *SharePoint*

$urlWeb1 = "http://sitecollection/site1/"
$guidList1 = "ProjectsGuid"

$urlWeb2 = "http://sitecollection/site2/"
$guidList2 = "CustomersGuid"

$isFieldRequired = $false #Or $true, depending on your needs

$web1 = get-spweb $urlWeb1
$list1 = $web1.Lists[[Guid]$guidList1]

$web2 = get-spweb $urlWeb2
$list2 = $web2.Lists[[Guid]$guidList2]

$list1.fields.AddLookup("NameOfTheNewColumn", $list2.ID, $web2.ID, $isFieldRequired )

Please use Guids!

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top