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.
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.
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:
if (this.ListItem[Field.Id] != null)
ChildDropDownList.SelectedValue = this.ListItem[Field.Id].ToString();
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:
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.
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.
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.