Daniel Reed
.NET, SharePoint and Graphic Design
Posted by mirata on Apr 9, 2012 in Download, SharePoint | 26 Comments

Download Mirata.CascadingLookup.wsp (13kb)

As you may notice, I’ve been away for a while. Constantly busy on other things – its always the same! In any case, here is my next free SharePoint component. A Cascading Lookup field. This wsp file is free for use. Of course I’ll always be keen to hear if you use it with anything interesting.

I’ve always been a little baffled by the fact SharePoint never included a Cascading Lookup field out of the box. It seems rather obvious to me that this would be a requirement for many people in Enterprise environments, especially for categorising things like document metadata. Not so long ago, I was working on projects for 2 separate clients that both had this requirement, and I figured this was my oppotunity to make some decent reusable components.

I hear some people ask – ‘why bother making this? Aren’t there some solutions on CodePlex that do this for you?’ Well, yes, there are a few - and I must take this oppotunity to give credit to http://cascddlistwithfilter.codeplex.com because it was a good starting point and I was able to borrow various concepts and code chunks to speed me up. But I think I can do better. Of all the solutions to the cascading problem I have seen, they all suffer from one or more of the following problems:

  • They are often difficult to set up. The more cascades involved, the harder it seems.
  • A full page refresh is required every time you select a value.
  • Other custom solutions usually require customisation with InfoPath or SharePoint Designer (eww).

So I’ve tried to address these features.

Using the Field

When installed, the user has a Cascading Lookup field type available to them. Lets use a practical example here – a typical Country, State, City scenario. Other lists may need this information, but naturally we don’t want to see every city on the planet each time we add a new item. We want it filtered. So we will build several lists. Countries, States and Cities.

Firstly we would create a regular custom list Called Countries, and specify each name in the Title Field.

Then we will create a second list – States. We will add a Cascading Lookup field called Country and specify the Source List as Countries. Title will be selected by default. Leave Parent Join blank, because we want to see all Countries.

Its the third list – Cities – where we really start to use the cascading features. State depends on Country. We create a County field just as we did in the States list. Then we create a second Cascading Lookup field called State. Specify the States list as the Source List. Then select Country as the Parent Lookup. Normally we might have to specify the source list’s Cascaing Lookup field to connect the dots, but since it has the same name in this case, it will be automatically selected.

To test the lookups, add some values. The State dropdown should be correctly filtered.

This scenario can be extended to many more dropdowns, using these fields in an Employees list for example. We can also use filtering options to only show values starting with ‘A’ for example.

Field Type

Ideally, I would like this field to be built upon SPFieldLookup. Unfortunately I spent a long time on it and encountered many random issues due to the way the parent class is implemented, particularly with things like required field validation and change detection. As such, the parent field is actually SPFieldText. However, for consistency, I still format the data like a lookup field. So if you ever decide to manually retrieve the value with code, you can pass the value to an SPLookupFieldValue class to split the value and text.

Filtering is done by a CAML query so that only the relevant items show up. This has been extended to include some additional filters.

AJAX

The field supports AJAX refreshing. I probably could have used the SharePoint Client Object Model, but for one reason or another opted for .NET AJAX. It is written in such as way that it detects its parent. This means a hierarchy of AJAX controls is built up automatically. This is done by every field determining its parent and child dropdown relationships on creation.

if (!string.IsNullOrEmpty(parentName))
{
  CascadingLookupControl parent = FindCascadingLookupControl(parentName);
  parent.AddChild(this);
}

Then later on, when a dropdown field is changed, the appropriate update panels are refreshed.

ch.CascadingDropDownUpdatePanel.Update();

if (ch.Children.Count > 0)
{
  foreach (CascadingLookupControl child in ch.Children)
  {
    PopulateChild(child, ch);
  }
}

XSL

SharePoint 2010 has a new way of rendering field values on list display screens. Obviously simply displaying my field’s internal text values would not be appropriate because we woud see the raw data.

We want to see a human-readable view. Microsoft provides documentation on how to do this at http://msdn.microsoft.com/en-us/library/ff606773.aspx. An XSL stylesheet is supplied to transform the data and trim the unnecessary information.

<xsl:stylesheet xmlns:x="http://www.w3.org/2001/XMLSchema" xmlns:d="http://schemas.microsoft.com/sharepoint/dsp" version="1.0" exclude-result-prefixes="xsl msxsl ddwrt" xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" xmlns:asp="http://schemas.microsoft.com/ASPNET/20" xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:SharePoint="Microsoft.SharePoint.WebControls" xmlns:ddwrt2="urn:frontpage:internal" ddwrt:oob="true">
  <xsl:output method="html" indent="no"/>
  <xsl:template match="FieldRef[@FieldType='CascadingLookup']" mode="Text_body">
    <xsl:param name="thisNode" select="."/>
    <xsl:variable name="fieldValue">
      <xsl:value-of select="$thisNode/@*[name()=current()/@Name]"/>
    </xsl:variable>
    <xsl:value-of select="substring-after($fieldValue, ';#')"/>
  </xsl:template>
</xsl:stylesheet>

Final Notes

This entry is somewhat rushed so I may have omitted some details. But if you are intertested in any particular areas of the development, post a message and I will attempt to expand it. And if enough people are interested, I will post the source later.

26 Comments
  • Hi Daniel,

    Congrats for your decision to share some of your code.

    I saw that one and think it´s good for us, but, before we publish any external component we need to check the source code.

    It´s possible to share the source of that component?

    thanks

    Rodrigo

  • Having some issues activating only this WSP file… any suggestions?

    • Yes I should probably give some installation instructions huh :) just install the wsp and do an iisreset. It is a control template and doesn’t need a feature to activate it.

  • Great soulution and work perfect!
    Could you write some words about installation?

  • It sound interesting, but I get an error when trying to download the wsp.
    It is still available?

    • I’m currently out of own, but when I get back ill check this for you.

  • hi,
    I am looking for a tree like filtering, where we don’t have fixed levels of cascading.
    some times we have two levels, and some times we have 5 levels.
    I don’t care if the lookup field reference one or many lists. it is ok for me the column it self is a textfield, my main concern is to show a tree during new and edit forms.

    could direct me to any direction?

    regards.

    • I don’t understand the exact details of your requirements but I imagine you could use an out of the box Managed Metadata field.

  • Hi …could you provide us the source code ….also if possible please try to explain this in brief.

  • How can i use this field in a page layout edit mode panel?

    • I haven’t tried it myself, but you would probably have to instantiate the custom control itself and set the values. I have done this in the past for other page layout edit fields when the generic fieldcontrol doesnt do what I want.

  • I have an issue, in test server I have all the functionality.. but in principal server the cascade columns do not respond… I select column A and column B just do not display cascade options?

    Have you seen this before?

  • I have used this in a custom list but I am unable to edit properties on the column once I have created it, getting the usual “Unexpected error” and I’m unable to enter maintenance mode to delete the column.

    If I get this to work would you expect the cascade feature to work in datasheet view?

  • Thanks for the effort of sharing this.. but Source Code would be nice to have.. I guess you should share it without asking :-)

  • i had deploy this wsp file on sharepoint 2013 but i not found related feature on site or sitecollecion features what is the cause

  • Hi Dan,
    thanks for sharing this. It is very useful. I am looking for a way to use metadata with cascading dropdown selection, instead of using multiple lists..

    do you think it can be done? will this be your next enhancement on this:-)?
    thanks,
    Ruiming

  • Another issue, the scenario of move the site collection to another server is supported by the solution? … when I restore the entire site and update the option “Use Custom Site” for the cascading lookup fields but it not start the functionality… do you have a workaround to resolve this problem?

    • I’d need to see what you mean by ‘not starting the functionality’. I can say that the fields do not store site-specific information, so the problem would only exist at a field configuration level. If you can get to that screen and configure the field without any errors, you should be able to make it work. Let me know.

  • deployed it on SP2013 but when i want to add a city, i don’t see the dropdown list of the cascading lookup fields (country / state)

  • First of all thanks for your wsp. Can you share the Source Code to me?

    Appriciate your effort!!!!!!!!!!!

    thanks
    Santhosh G

  • I installed your .wsp in a SharePoint 2010 – environment and it works fine! Thanks a lot! Next year, we plan to migrate our sites to SharePoint 2013. My question is now: will it still work in SP 2013 ?

    • Unfortunately no. I was just saying on a similar post in this thread – SharePoint 2013 uses fields in a slightly different way. I may release a new version.

      • I did some tests in SP2013 and I worked but maybe accidental. I’ll wait for your new version.

  • Hello,

    It’s possible to publish the source code? I’m some difficulty setting this solution onto a SharePoint 2013 installation. If you could put the source available it would be nice.

    Thanks

    • I’ll see if I can dig it up soon. I can tell you that SharePoint 2013 works differently, as most of the controls now use JavaScript to interact. As such, this solution will not work. But if I remember, there is an option to switch it back to the old way of doing things.

      • Is there now a release for SharePoint 2013. What do you mean with “there is an option to switch it back to the old way of doing things ?”

Leave a comment to mirata

Spam Protection by WP-SpamFree