Saturday, February 12, 2011

Microsoft InfoPath 2010 XML Drill-Down


Just wanted to throw a quick fix out there for a common problem I see with InfoPath. Often times InfoPath developers find themselves wanting to drill down information based on a selected field in a drop down list. Typically this information is stored in XML format (if it isn't, it gets messy in a hurry).
I'm using InfoPath 2010 in this example.
Before I jump into the how-to, I'll give you a look at my XML source:

It's pretty primitive, but for the purpose of this document, it will work.
The way my particular form is set up is just a drop down list and a text box. You could expand on this an infinite amount of times if you wanted to (that might be a long day).

The first step is to set the data source for the drop down list. You do that by right clicking on the dropdown list, and clicking on Drop-Down List Properties then under the List Box Choices heading, select the third radio button – Get Choices From External Data Source. Then click Add to add a data source. Here, you need to navigate to your XML file that is storing the information. If you XML file is formatted properly, you should see a list with "folders" and then values inside them. Here is what mine looks like:

Select the value that you want to populate the drop down list. Because I wanted to retrieve the ShipsNumber when the user selects the name of the ship, I'm selecting Name for this one.
Now you can "OK" out of everything and go to your rule manager for the drop down list. You'll create a new action that will fire when the list box is changed. We're going to Set a fields value when this rule runs. We want to set the value of the textbox field (which I named "ShipNumber") with this rule.

Now here comes the tricky part. We need to set it to the corresponding ShipNumber of the ship that was selected. To do this, we have to apply a filter as follows.
Click on the Fx (formula) button to the rite. It will bring up a new dialog box where we can enter a formula:

We're then going to click Insert Field or Group:
When it brings up the list, make sure that the XML file is selected as the field source; as highlighted below. Here we select the value that we want to drill down. In my case, that is ShipNumber.

Now click on Filter Data and yet another dialogue box will appear. We want to Add a new filter and yet ANOTHER dialogue pops up (only 1 more, I promise). Here is where we declare the expression for the filter. I want to show the ShipNumber where the Name is equal to the name that the user selected in the drop down list:

When the final dialogue box appears, you simply select the drop down list and you're good to go.



When you first start doing this, it can get a little ugly as the rabbit hole of dialogue boxes keeps getting deeper and deeper. I hope by using an excess of pictures, it'll be a little easier to figure out. Be sure to post if you have an questions.
Best of luck!

No comments:

Post a Comment