Sun, Aug 5 2007 19:20 Ben Walters

Filtered Drop Down Lists 101

A couple of weeks ago I received an email from Dominic asking for help with creating filtered drop down lists in InfoPath. So I thought I would put together a couple of posts on how to achieve this functionality in both InfoPath 2003 and InfoPath 2007.

For this post I'm going to look at how to filter drop down data while using the InfoPath client, as filtering of data is not supported in browser based forms I will cover this in a separate post.

The first thing we are going to need is some data to filter, at the end of this post is a list of cars with their makes and models that I have used for this example. Simply take the data listed below and paste it into a new XML file called CarList.xml

So with our data taken care of it's time to create our form. Now for these examples I'm going to be using InfoPath 2007 however the same principals ring true when filtering data in InfoPath 2003. Open InfoPath and design a new form, match the data source to the one displayed below (note: the data type for each of the fields is simply text)

image

With the car repeating section created right click on the car group and select "Repeating table" to add a new repeating table to the form design.

image

Right click on the Make, Model, and Color text boxes and change them to Drop Down Lists

image

You should now have a form that looks like this

image

With our three drop down lists created we now need to fill them with data, open the properties dialog for the Make drop down list and select the "Look up values from an external data source" option.

Click on the Add button to create a new external data connection,

  • Select Receive Data Connection Click next
  • Select XML document then click next
  • Navigate to the XML Document you created using the Car Data below then click next
  • The select "Include the data as a resource file in the form template or template part option" then click next
  • Name your data source "CarList", Make sure the "Automatically retrieve data when the form is loaded is check then click Finish

You should now have a properties dialog that looks similar to the following

image

Click on the button next to the Entries: text box to select the data you want displayed in the drop down you will be presented with the field selector dialog, as we don't need to filter any of the Make data you can select the make node and then click ok

image

Make sure the show only entries with unique display names is checked (note: this feature is only available in InfoPath 2007, this means that you will need to structure your data slightly differently for an InfoPath 2003 form) then click the OK button to close the properties dialog for the "Make" drop down.

now comes the fun part setting up the filtered drop down lists, open the properties dialog for the "Model" drop down, and select the option to source data from an external data source (you will be able to use the existing connection to the CarList.xml file that we made earlier)

Click on the entries button to select the data to be displayed and select the Model field, before clicking ok click on the "Filter Data" button.

image

image

Click the "Add" button to create a new data filter.

image

Creating the filter is pretty straight forward, the first Drop Down represents the field that you are comparing, this case it is the "Make" node from our CarList.xml file. The second drop down represents the equation type that will be used Equal to, Not Equal to etc... The third and final drop down represents the field you want to compare to, in this example it's the "Make" field in our forms data source. When you click on the "Select a field or group..." option you will be able to select the "Make" field from the main data source.

image 

Click ok to close the "Select a Field or Group" dialog, and click OK on each of the remaining open dialogs to close them and commit their changes.

Now if we click on the preview button on our form we should see the filtered drop downs working.  image

image

The added bonus is that because our filtering is based on the current item it works just as effectively in a repeating list.

image 

For now this is where I leave you, you'll notice however that there is one more drop down in the form that has not been setup, I figured I would leave the Colour drop down for you to setup. Basically all you have to do is filter the Colour drop down data based on the selection in the Model drop down. Next time I'll show you how to achieve the same functionality in a web based form

Car list XML Sample

<CarList>
  <Cars>
    <Car>
      <Make>Ford</Make>
      <Model>XR6</Model>
      <Color>Blue</Color>
    </Car>
    <Car>
      <Make>Ford</Make>
      <Model>XR6</Model>
      <Color>Red</Color>
    </Car>
    <Car>
      <Make>Ford</Make>
      <Model>XR6</Model>
      <Color>Yellow</Color>
    </Car>
    <Car>
      <Make>Holden</Make>
      <Model>Commodore</Model>
      <Color>Green</Color>
    </Car>
    <Car>
      <Make>Holden</Make>
      <Model>Commodore</Model>
      <Color>Orange</Color>
    </Car>
    <Car>
      <Make>Holden</Make>
      <Model>Commodore</Model>
      <Color>Aqua</Color>
    </Car>
    <Car>
      <Make>Ford</Make>
      <Model>Focus</Model>
      <Color>Black</Color>
    </Car>
    <Car>
      <Make>Ford</Make>
      <Model>Focus</Model>
      <Color>Light Blue</Color>
    </Car>
    <Car>
      <Make>Ford</Make>
      <Model>Focus</Model>
      <Color>White</Color>
    </Car>
    <Car>
      <Make>Holden</Make>
      <Model>Astra</Model>
      <Color>Pink</Color>
    </Car>
    <Car>
      <Make>Holden</Make>
      <Model>Astra</Model>
      <Color>Cornflower Blue</Color>
    </Car>
    <Car>
      <Make>Toyota</Make>
      <Model>Camry</Model>
      <Color>White</Color>
    </Car>
    <Car>
      <Make>Toyota</Make>
      <Model>Camry</Model>
      <Color>Gold</Color>
    </Car>
    <Car>
      <Make>Toyota</Make>
      <Model>Camry</Model>
      <Color>Red</Color>
    </Car>
    <Car>
      <Make>Porsche</Make>
      <Model>Boxter</Model>
      <Color>Red</Color>
    </Car>
    <Car>
      <Make>Porsche</Make>
      <Model>Boxter</Model>
      <Color>Blue</Color>
    </Car>
    <Car>
      <Make>Porsche</Make>
      <Model>Boxter</Model>
      <Color>Black</Color>
    </Car>
    <Car>
      <Make>Porsche</Make>
      <Model>Carrera</Model>
      <Color>White</Color>
    </Car>
    <Car>
      <Make>Porsche</Make>
      <Model>Carrera</Model>
      <Color>Yellow</Color>
    </Car>
  </Cars>
</CarList>

Filed under: ,

# re: Filtered Drop Down Lists 101

Saturday, September 08, 2007 2:36 PM by David

I'd love to see the sample for use with web based forms. I know that filtering is non-supported in web based forms and I've seen some samples use quite complex code behind files, but I can't imagine that is the only way to do that.

Is there another less complex method?

# re: Filtered Drop Down Lists 101

Sunday, September 09, 2007 8:58 PM by Ben Walters

Hey David,

I'm in the process of writting up this post now. Should have it up in the next couple of days

# re: Filtered Drop Down Lists 101

Monday, September 24, 2007 9:22 AM by Tom

Hi,

Can't wait to see your solution for filtering drop down list for the web based forms using InfoPath.

# re: Filtered Drop Down Lists 101

Friday, October 26, 2007 9:46 AM by Dattard

I've blogged on how to do this using SharePoint 2007 (i.e. outside of InfoPath)

dattard.blogspot.com/.../sharepoint-2007-filtered-lookups.html

# re: Filtered Drop Down Lists 101

Friday, January 18, 2008 5:15 AM by Carson

Hi- great post...

BUT - i have a question/challenge:

I have cascading/drop-down with unique filtering working just fine.

but when i try to replicate the same into a repeating table, the filtering doesnt work...

Any hints?

# re: Filtered Drop Down Lists 101

Wednesday, March 19, 2008 6:11 PM by Bob

Ben,

I've followed your directions and I get a "Infopath has encountered an error. The operation failed. Unspecified error" message at the final step of setting up the data connection for the MAKE drop-down list. I've setup my CarList.xml file as specified.

I'm using Infopath 2007. Any thoughts. Unfortunately I can't attach the screenshot for you.

Thanks

# re: Filtered Drop Down Lists 101

Friday, March 21, 2008 12:34 PM by Brad Covell

Great post. Just what I was looking for.

# re: Filtered Drop Down Lists 101

Tuesday, July 15, 2008 4:08 PM by Jon Taylor

Can you be a bit more specific with the data structure required for InfoPath2003? You mention it with the unique name feature that's not available. I was able to get the first pull down to take unique names, but then I get all choices in the second field. I've tried filter make = make focusing on changing the left side object. I haven't found any combination (main or secondary) that works.

# re: Filtered Drop Down Lists 101

Wednesday, July 16, 2008 7:31 PM by Patrice

A problem here is that when a value is changed in a dropdown it doesn't remove (or clear) the options selected in the other boxes that are not values associated with the new value selected.  So you would have to somehow clear the lists when values are changed.  Or am I missing something here?

# re: Filtered Drop Down Lists 101

Wednesday, July 23, 2008 3:58 AM by Ben Walters

Hey Jon,

InfoPath 2003 doesn't provide the "Display only Unique entries" feature. With this in mind you will need to structure your data similar to the following

<CarList>

<Makes>

 <Make>Holden</Make>

 <Make>Ford</Make>

 <Make>Porche</Make>

</Makes>

<Models>

 <Model make="Ford">XR6<Model>

 <Model make="Ford">XR8<Model>

 <Model make="Ford">Focus<Model>

 <Model make="Porche">Boxter<Model>

 <Model make="Holden">Astra<Model>

</Models>

<Colors>

 <Color make="Ford" model="XR6">Red</Color>

 <Color make="Ford" model="XR6">Blue</Color>

 <Color make="Holden" model="Astra">Silver</Color>

 <Color make="Porche" model="Boxter">Black</Color>

</Colors>

</CarList>

# re: Filtered Drop Down Lists 101

Wednesday, July 23, 2008 4:00 AM by Ben Walters

Hey Patrice,

The best way to clear your previously selected options, is to use a rule on your drop down list that when fired clears any cascading drop down lists.

If you need more details on how to this feel free to let me know

Cheers,

# re: Filtered Drop Down Lists 101

Wednesday, September 24, 2008 12:49 PM by Lori

Does anyone know how to filter a drop down list when the data is coming from a secondary source?

# re: Filtered Drop Down Lists 101

Wednesday, September 24, 2008 6:29 PM by Ben Walters

Lori

The principal is still the same, however rather than filtering the results in a web service you will need to add code to the form to do the filtering for you.

If you have a large result set potentially comming back it might be best to use a Web Service as an intemediate step to reduce the data being returned.

Hope this helps

# re: Filtered Drop Down Lists 101

Monday, January 26, 2009 2:04 PM by Frankie D.

How can I submit all the repeating table data to a sharepoint list ? I have a submit set up, but only the first lines gets submitted.

# re: Filtered Drop Down Lists 101

Monday, June 22, 2009 9:49 AM by Mazen

I need an answer for th first post please

"I'd love to see the sample for use with web based forms. I know that filtering is non-supported in web based forms and I've seen some samples use quite complex code behind files, but I can't imagine that is the only way to do that.

Is there another less complex method?"

# Web Based Forms

Tuesday, July 07, 2009 8:25 AM by Pieter Theron

Hi Ben,

can you please post a link to an example of how to use this in a web based form?

Thanks Pieter

# re: Filtered Drop Down Lists 101

Tuesday, July 07, 2009 8:44 AM by Ben Walters

Hey Pieter, check this follow up post

msmvps.com/.../filtered-drop-down-lists-101-part-2.aspx

Cheers

Ben

# Drop down lists & repeating sections

Friday, July 31, 2009 5:05 PM by Terry Boulter

Ben,

Hope you can help as I'm a bit stuck!!

I'm fairly ok at InfoPath supported by jscript etc. I've developed a form that has repeating sections which basically ask questions with dropdown list answers. The list is populated from an external xml file (created, managed & maintained as another Ip form) The repeating section (in the main form) is constructed on a button command. the repeating section has a variable called counter with a corresponding variable in the external file. On this basis I populate the repeating section dropdown (from a repeating table in the external file)  using something like :-

externaldatasource/answers[local counter = external counter]

answers is what appears in the local dropdown list.

This nearly works fine, however, if there's an answer in section[2] that appears in section[1] then section[1] data is displayed.

Sorry please bear with me! for example if section[1] (local)dropdown (popultated from external xml) has Yes & No & section[2] has Yes & Maybe - then the Maybe is ok i.e. I can reliably get the other external repeating table data, but the Yes answer returns the  associated data relating to section[1]. It's feels like the data is cached in someway.

Local

<questiondata> - repeating

 counter

 question

 Answer - string

   desirability - attribute of answer

External

<data> - repeating

 counter (cut across from external using jscript on button command)

 question

 <Answers> - repeating

    Answer

    Desirability

In essence the external holds the question, the possible answers and corresponding desirability that i want the local file to read.

I understand .not, preceding, forward sibling etc, using none of which seem suitable. It's like I want to purge any cached data on the reading of data in each local sectionNo.

Hope this understandable/reproducable and fixable.  I've searched forums etc. to no avail. - you fix this and i'll arrange for Aus to win the Ashes!

terryboulter@aol.com

# re: my previuos post

Sunday, August 02, 2009 9:17 AM by Terry Boulter

Sorry for wasting anyone's time who tried to understand earlier post - problem now resolved - error on my part. It's the use of current() that I hadn't quite got correct.

# re: Filtered Drop Down Lists 101

Wednesday, January 06, 2010 3:04 AM by kensou09

very helpful..but how about implementing these (Make, Model and Color) using sharepoint? You only provided a tutorial on XML..i have a problem on what to put on my list in sharepoint.  :)

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above: