Create Cross-site Lookup column in SharePoint

Recently there is new requirement from client to add lookup column to a list in the other site. In order to do this, one of the solution is to add site column in root site, so it can be used by all its sub sites.

If we create a lookup site column from root site, the source list in sub site is not available from “Get information from” section in column settings.

A quick solution is to create a lookup site column and manage code to link the column with list in sub site when feature activated.

Below is my Site column Elements.xml looks like:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
 <Field Type="Lookup"
      DisplayName="ClientNo"
      StaticName="ClientNo"
      Name="ClientNo"
      Required="TRUE"
      EnforceUniqueValues="FALSE"
      List="Lists/Client"
      ShowField="Title"
      UnlimitedLengthInDocumentLibrary="FALSE"
      RelationshipDeleteBehavior="None"
      ID="{468A862E-183B-4F5C-A59F-8BA608117C09}"
      RowOrdinal="0"/>
</Elements>

In your Feature Receiver code:

public static class SPFieldLookupExtensions
{
   public static void UpdateLookupReferences(this SPFieldLookup lookupField, SPWeb web, SPList list, string sFieldName)
   {
      if (string.IsNullOrEmpty(lookupField.LookupList))
      {
         lookupField.LookupWebId = web.ID;
         lookupField.LookupList = list.ID.ToString();
         lookupField.LookupField = sFieldName;
         lookupField.AllowMultipleValues = true;
         lookupField.UnlimitedLengthInDocumentLibrary = true;
      }
      else
      {
         lookupField.SchemaXml = lookupField.SchemaXml.Replace("Lists/Client", list.ID.ToString());
         lookupField.LookupWebId = web.ID;
         lookupField.LookupField = sFieldName;
         lookupField.AllowMultipleValues = true;
         lookupField.UnlimitedLengthInDocumentLibrary = true;
      }
      
      lookupField.Update(true);
   }
}

[Guid("76335e5e-5365-4d4d-a1a2-e09c9d059fc5")]
public class CustomFeatureEventReceiver : SPFeatureReceiver
{
   public override void FeatureActivated(SPFeatureReceiverProperties properties)
   {
      try
      {
         // Get root site and web that list exists
         SPSite site = properties.Feature.Parent as SPSite;
         using (SPWeb web = site.OpenWeb(new Guid("{24a0bf4c-1234-4e78-5678-ebb42afb46e1}")))
         {
            if (web.Lists["Client"] != null && site.RootWeb.Fields["ClientNo"] != null)
            {
               SPList listClient = web.Lists["Client"];
               SPFieldLookup field = (SPFieldLookup)site.RootWeb.Fields["ClientNo"];
               field.UpdateLookupReferences(web, listClient, "ClientNameAndNumber");
            }
         }
      }
      catch (Exception ex)
      {
          // Error handler
      }
   }
}

This will do the work and hopefully it helps you.
But then, there is a more sexier solution that I found recently and you could use it for FREE.
DAMN … he is darn good.

Check this out: http://www.codeproject.com/Articles/619118/Cross-site-lookup-column-for-SharePoint-2013-and-S

Advertisements

Tagged: , , , , , , , ,

3 thoughts on “Create Cross-site Lookup column in SharePoint

  1. Dam ii 02/24/2014 at 10:12 AM Reply

    Lookup Plus for SharePoint 2013, It is more than Sharepoint Lookup. Cascaded Lookup, Filtered lookup, Cross-site Lookup /drop down/ and
    some controls are free. (“Create new item” link) Visit, http://www.azu.mn
    Or watch the channel http://www.youtube.com/watch?v=70-hXWY6ARY&list=PL-5v3e65O1cFMddgCpzIURE_UAsL-oDdW

    • tjenho 02/24/2014 at 1:07 PM Reply

      Great, nice to know.

  2. Dirk-Jan van Vliet (@djvanvliet) 06/17/2014 at 8:07 AM Reply

    It is unfortunately not for free, the Cross-site Lookup for SharePoint 2013 for example costs $299,-.

Leave a Reply

Please log in using one of these methods to post your comment:

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

%d bloggers like this: