{"id":854,"date":"2012-10-01T11:22:45","date_gmt":"2012-10-01T16:22:45","guid":{"rendered":"http:\/\/osric.com\/chris\/accidental-developer\/?p=854"},"modified":"2014-01-30T11:47:09","modified_gmt":"2014-01-30T16:47:09","slug":"sharepoint-2010-cross-site-lookup-using-powershell","status":"publish","type":"post","link":"https:\/\/osric.com\/chris\/accidental-developer\/2012\/10\/sharepoint-2010-cross-site-lookup-using-powershell\/","title":{"rendered":"SharePoint 2010 Cross-Site Lookup using PowerShell"},"content":{"rendered":"<p>A common SharePoint request is to create a lookup field from a list in another site. This is possible so long as the sites are within the same site collection, but it does require some customization.<\/p>\n<p>Since I am dealing with a single-server farm, I was able to do this fairly quickly in PowerShell:<\/p>\n<p><code># Set the site collection in which the source and destination webs exist<br \/>\n$site = Get-SPSite(\"https:\/\/mysite\/path\/\")<\/p>\n<p># Set the source web to the web where the source list exists<br \/>\n$sourceweb = $site.OpenWeb(\"sourcepath\")<\/p>\n<p># Set the destination web to the web where you wish to add the lookup<br \/>\n$destinationweb = $site.OpenWeb(\"destinationpath\")<\/p>\n<p># Set the source list to the list in which the field exists<br \/>\n$sourcelist = $sourceweb.Lists[\"List Name\"]<\/p>\n<p># Set the source field to the field you wish to perform lookups on<br \/>\n$sourcefield = $sourcelist.Fields[\"Field Name\"]<\/p>\n<p># Get the collection of all fields in the destination list<br \/>\n$destinationfields = $destinationweb.Lists[\"List Name\"].Fields<\/p>\n<p># Add a lookup field to the fields collection<br \/>\n# See the SPFieldCollection AddLookup(String,Guid,Guid,Boolean) method<br \/>\n# For details: http:\/\/msdn.microsoft.com\/en-us\/library\/ms430950.aspx<br \/>\n$lookupname = $destinationfields.AddLookup($sourcefield.Title,$sourcelist.ID,$sourceweb.ID,false)<br \/>\n# The above produces an error: Missing expression after ','. At line:1, char:83<br \/>\n# Right--in Powershell, $false is False.<br \/>\n$lookupname = $destinationfields.AddLookup($sourcefield.Title,$sourcelist.ID,$sourceweb.ID,$false)<\/p>\n<p># Get the newly-added lookup field<br \/>\n$lookup = $destinationfields.GetFieldByInternalName($lookupname)<\/p>\n<p># Set the lookup field to the internal name of the source field<br \/>\n$lookup.LookupField = $sourcefield.InternalName<\/p>\n<p># Update the lookup -- without this the previous changes won't be saved<br \/>\n$lookup.Update()<\/p>\n<p># Dispose of the SPWeb and SPSite objects<br \/>\n$sourceweb.Dispose()<br \/>\n$destinationweb.Dispose()<br \/>\n$site.Dispose()<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A common SharePoint request is to create a lookup field from a list in another site. This is possible so long as the sites are within the same site collection, but it does require some customization. Since I am dealing with a single-server farm, I was able to do this fairly quickly in PowerShell: # &hellip; <a href=\"https:\/\/osric.com\/chris\/accidental-developer\/2012\/10\/sharepoint-2010-cross-site-lookup-using-powershell\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">SharePoint 2010 Cross-Site Lookup using PowerShell<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[180],"tags":[228,211],"class_list":["post-854","post","type-post","status-publish","format-standard","hentry","category-sharepoint-2","tag-powershell","tag-sharepoint-2010"],"_links":{"self":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/854","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/comments?post=854"}],"version-history":[{"count":11,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/854\/revisions"}],"predecessor-version":[{"id":1049,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/854\/revisions\/1049"}],"wp:attachment":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/media?parent=854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/categories?post=854"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/tags?post=854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}