SharePoint 2010 Cascading Lookup Fieldtype

A few months ago at work I was tasked with heading up the development of one of our more complicated sites (research.wustl.edu) while we moved it from SharePoint 2007 to SharePoint 2010. These moves are generally pretty easy, but they can be complicated when the site relies on a lot of customized code that might not work so well in the new environment. As irritating as it can be at times, this particular case gave me the opportunity to create something new, something that I hope can benefit someone else in a similar situation.

[Skip all this and go straight to the code on CodePlex]

One of the requirements for this site was a pair of cascading lookup fields. This was achieved in 2007 with a 3rd party solution that created a pair of custom lookup fields that filtered off of one another to create a cascading effect. Unfortunately we were not able to get that some solution working in our 2010 environment, so we started looking at other options.

We first looked to see if we could find another 3rd party solution that would accomplish this goal for us, but all we could find were JavaScript implementations that didn’t manage the data the way we needed to. We did find a couple of options that seemed like they might work, but they were closed source (we don’t like to put 3rd party software on our servers without knowing the code if we can possibly avoid it) and fairly expensive. So I decided since we had access to the source code of the 2007 solution that I’d try to rebuild a version of it that would work in 2010.

The first thing I did was track down the solution that our old 2007 solution had itself been based on, a somewhat simpler version by DataCogs. I used this code as the base, and set about building a SharePoint 2010 solution that would incorporate all the functionality we had in 2007.

I was able to keep most of the old solution in tact, but there were a few source code modifications I had to make in order to get the two columns to update properly. The biggest issue was when editing list items, the child dropdown wouldn’t display the correct value. It would simply display the first available item in its index. Once I realized what was going on, the solution was pretty simple:

It doesn’t run on a page postback because changing the parent dropdown triggers a postback, and also changes the available options for the child dropdown. So if that code ran after a postback, the child field would try to set its SelectedValue to something that wasn’t an option and would throw an error.

Once deployed, setup is very simple. Just create a two new columns, and choose the new parent and child dropdown column types:

index

The setup for the parent type is very simple.

  • Enter the URL of the subsite the source list is a part of.
  • Click “Load Lists” to populate the next dropdown with all available lists on that subsite.
  • Choose your source list from the dropdown.
  • Choose the columns whose values you want to use for each dropdown item’s display name and value.

parentsetup

The setup for the child type is almost exactly the same. The only difference is the final selection that determines which column in the child source list will be used to filter against the value of the parent list item.

childsetup

If setup were completed as laid out in the images above:

  • The parent column would display all items from the “Pages” library, using the “Name” column to display to the user and the “Title” column to store the value to the list.
  • The child column would display items from the “Documents” library, using the “Name” column to display to the user and the “Title” column to store the value to the list.
  • The child column would ONLY display items in which the column “ID” matched the “Title” column of the item selected in the parent field.

The Visual Studio solution for this project and the WSP to deploy it are available on CodePlex.

Tagged with:
Posted in .NET, .NET for SharePoint, CodePlex, Server Side Code, SharePoint
One comment on “SharePoint 2010 Cascading Lookup Fieldtype
  1. Hamed says:

    Hi
    Thanks for your solution but after I create fields,when I want to open newform this error appears:
    ParentDropDownList is null. Problem with ParentDropDownListFieldControl.ascx file.
    similar of this error is for child dropdownlist.
    My sharepoitn version is 2010.
    could you help me?

Leave a Reply to Hamed Cancel reply

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

*