Tag Archives: lookup

Adding a lookup field to your SharePoint solution: declaratively or programmatically?

Big thanks to Chris Barbin on pointing me in the right direction for this one!

Including a lookup in your project can be tricky, and I find that most developers revert to doing it programmatically instead of declaratively. It’s easier to copy and paste some C# code than chase down issues around CAML. It can be incredibly aggravating and time consuming chasing down these little annoyances with SharePoint. Anywoo, I really wanted to make sure my project did it the right way, and with Chris’s help, I was successful!

Why bother trying declaratively?

Using SharePoint’s CAML/XML, the field is easier managed, SharePoint has to think a little less as it just reads and parses an XML file, which, if you explore the hive you’ll see virtually EVERYTHING is setup that way. Also, in SharePoint 2013, sandbox solutions only support declarative customizations, it’s true, no more codeSoooo I think SharePoint has made it rather clear, and creating it correctly now will get you in the right mindset as well as help when upgrading into SharePoint 2013.

As a quick disclaimer: obviously, if you’re not defining your list and you need to add a lookup to an existing list, it’s going to be easiest to add it to the existing list programatically. I don’t think it’s possible delcaratively.

For my scenario, let’s add 2 new lookup columns to a document library, so we can use SPServices for cascading some categories.

Doing this programmatically would look something like:

SPList SharedDocumentslist = web.Lists.TryGetList("Shared Documents");

SPList categoryList = web.Lists.TryGetList("Categories");
 SPFieldLookup categoryField = (SPFieldLookup)SharedDocumentslist
    .Fields.CreateNewField(SPFieldType.Lookup.ToString(), "Category");
 categoryField.LookupList = categoryList.ID.ToString("B");
 categoryField.LookupField = "Title";
    true, SPAddFieldOptions.AddToAllContentTypes);

SPList subCategoryList = web.Lists.TryGetList("Sub Categories");
 SPFieldLookup subCategoryField = (SPFieldLookup)SharedDocumentslist
    .Fields.CreateNewField(SPFieldType.Lookup.ToString(), "SubCategory");
 subCategoryField.LookupList = subCategoryList.ID.ToString("B");
 subCategoryField.LookupField = "Title";
    true, SPAddFieldOptions.AddToAllContentTypes);


Meh, 15+lines of code (you better have some error handling, and the trigger to run this code, so you’re probably looking at close to 40+ lines and a few different files). That’s a lot of overhead for something that can be done in a couple of lines declaratively. Check this out:

In my library’s list schema, I want to add two field tags, to the <Fields element:

<Field ID="{dbd44429-8804-458d-b86f-122141fb1070}"
<Field ID="{8C8CD65E-F2BB-45B7-8835-CC2BF97C9D5E}"
List="Lists/Sub Categories"
ShowField="Title" />

Easy enough right? 2 lines (I know above shows many more, but it can be formatted for 2 lines ;) of manageable XML vs 40+ lines of C# code… tell me which one is best.

Big Note: These field elements are added to the list’s schema, these are not site columns. Something goes terribly awry making these site columns first. I’ve read others making them site columns and then using some code to handle it at the list. If you require site columns for content aggregation go for it. If I’m missing something and you can get this to work with site columns, please share!

Til next time, Happy SharePointing!


Creating an item detail dashboard view of your joined lists in SharePoint 2010

Thanks to Stan for suggesting the idea for this post, by leaving a comment here.

Our goal here is to take your joined lists and display them on a single page, so you can effectively review an item and its associated information. This is a useful task for creating views of projects with many tasks, milestones, documents, or customers with contacts, documents, or products with updates, documentation, downloads. Really, anywhere a list that is linked (via a lookup field) to another list, this can be applied.

First, let’s assume the following: we have a parent list, and a few daughter lists which have lookup fields to the parent list.

Parent and Daughter Lists

Simple enough. In my example here, I am going to use a project theme. I have a Projects list, a Tasks list, and an Issues list.

Starting with the parent list, first let’s disable the popup dialog option. We can’t edit the pages in a dialog window, plus you’ll want a nice full page to view all your info. Go to the parent list, click List in ribbon > List Settings > Advanced settings. At the bottom of the page, Select No under Launch Forms in Dialog. Click OK.

Browse back to your project list and select one of your projects. You should see the nice generic page view.

Press Site Actions in the top right, then select Edit Page. The page will switch to edit mode. Now go ahead and add your lists you wish to join. In my case, I am going to add Tasks and Issues. Your new web parts will be added to the top of the page. Make sure to drag your web parts down so your project details remains at the top.

Adding web parts to project display page

Once your web parts are added, edit each web part and set the Chrome Type (under Appearance) to Title and Border or Title Only, your choice. Make sure to click OK at the bottom of the edit web part panel to save your changes.

While we’re still in edit page mode, click the web part edit menu for your first web part (same place you went to edit the web part) and select Connections > Get Filter Values From > Projects.

Add the connection for the web part

A new dialog will appear.

Web part connections dialog

For the Provider Field Name, select ID. For the Consumer Field Name, select the  lookup field, Project. Click Finish.

Create the web part connection

Your web part will be reloaded and filtered based on your project.

Filtered connection web part

Perform these same connection steps for the other web parts on your page.

When you’re done, press Page ribbon, then Stop Editing. That’s it!

As you click and view each project, your tasks and issues will automatically filter for the selected project. Pretty sweet eh?


Take it further!

  • Edit the display form in SharePoint designer to add 2 columns to your page, allowing you to put additional web parts to the right of the details as well.
  • Using color coded calendars, see my post here, you can create a calendar view of your tasks and projects, color coded based on status. Create a calendar view in each list, then when creating the color coded calendar, use these lists and views!
  • Create a calendar for your tasks, color coded based on status, and add another web part to the above page to include the calendar!

Happy SharePointing!