Skip to main content

Cloud

Lookup field with Custom Display Text in SharePoint 2010

Let’s say you want to create a SharePoint lookup field where the dropdown options display something other than title of the lookup item…
If the field you want to show is a single line of text type, then you’re in luck! Here’s an example of how straighforward it is.
However, if you want to be able to do a lookup field that displays  anything else, like a URL of a page in a Pages library or a calculated value, that’s not going to happen out of the box. The easiest thing to do is to use OOTB SharePoint functionality to create a placeholder field that stores the information you want to display in your lookup but in “single line of text” form. You can also build a custom field and a custom control to display exactly what you want. In some cases this may be necessary, but if all you want is a custom display value in a dropdown, I think that’s overkill: harder to maintain, debug, etc.

Example

In this example I’m going to add the lookup field to the Lookup List and its information will come form the Information List. My lookup field will display the URL of items in the Information List. (If copy-pasting code out of these boxes is a pain, I’ll be happy to send you the code as a text file.)

    1. Create the text field to hold your lookup text in the source list.
   1:

   2:         const string lookupListName = "Lookup List";

   3:         const string infoListName = "Info List";

   4:

   5:         public override void FeatureActivated(SPFeatureReceiverProperties properties)

   6:         {

   7:             SPWeb web = SPContext.Current.Web;

   8:             #region create lists

   9:             DeleteLists();

  10:             web.Lists.Add(infoListName, "Information List", SPListTemplateType.GenericList);

  11:             web.Lists.Add(lookupListName, "Lookup List", SPListTemplateType.GenericList);

  12:

  13:             #endregion

  14:

  15:             #region the good stuff

  16:             // pick your lists

  17:             SPList destinationList = web.Lists[lookupListName];

  18:             SPList sourceList = web.Lists[infoListName];

  19:

  20:             // create the text field named Display Info Column Name. 

  21:             string displayColName = sourceList.Fields.Add("Display Info Column", SPFieldType.Text, false);

  22:

  23:             // create the lookup to pull information out of that field you just created. 

  24:             string lookupColName = destinationList.Fields.AddLookup("Lookup Column Name", sourceList.ID, false);

  25:             SPFieldLookup lkp = (SPFieldLookup)destinationList.Fields.GetFieldByInternalName(lookupColName);

  26:             lkp.LookupField = sourceList.Fields["Display Info Column"].InternalName;

  27:             lkp.Update();

  28:

  29:             #endregion

  30:

  31:             // add columns to default views to make it easier to verify that everything was created properly

  32:             SPView view1 = sourceList.DefaultView;

  33:             view1.ViewFields.Add(displayColName);

  34:             view1.Update();

  35:

  36:             SPView view2 = destinationList.DefaultView;

  37:             view2.ViewFields.Add(lookupColName);

  38:             view2.Update();

  39:

  40:         }

  1. Create the lookup field to do the lookup in the destination list.
   1: public override void ItemAdded(SPItemEventProperties properties)

   2:         {

   3:             using (SPWeb web = properties.OpenWeb())

   4:             {

   5:                 base.ItemAdded(properties);

   6:                 if (properties.List.Fields.ContainsField("Display Info Column"))

   7:                 {

   8:                     properties.ListItem["Display Info Column"] = properties.ListItem.Title + " " + DateTime.Now.ToShortDateString();

   9:                     properties.ListItem.Update();

  10:                 }

  11:             }

  12:         }

  1. Create an event receiver that automatically populates the lookup text.

custom-lookup-project

Random note: when creating the event receiver, beware of ItemAdded in the pages library. It won’t want to let you update the item because of a conflict described in this blog. If you are working with  the pages lib, use ItemAdding instead.

Thoughts on “Lookup field with Custom Display Text in SharePoint 2010”

  1. Many of the processes are the same within SharePoint 2013 but there are some new features that need to be learned. Upgrading to SharePoint 2013 is recommended and training is provided in order to make the most of the platform.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Darya Orlova

More from this Author

Follow Us