Free Training: Data-Driven Design - Using XML Files to Configure Other Forms (Lab 2) - Mel Balsamo
in

InfoPath Dev

This Blog

Syndication

Mel Balsamo

Free Training: Data-Driven Design - Using XML Files to Configure Other Forms (Lab 2)

In this lab, your business process has evolved and you have many more questions and answers. You decide using a configuration form is more efficient so you'll modify your Quiz Form from Lab 1. In real life, you could also create a form that pulls from a configuration form without going through the steps in Lab 1.

Create a Form out of your Template

1.       Open your Quiz Form template in InfoPath Designer.

To make sure you are not overwriting your template for Lab 1, change the title and save it with a different name.

2.       Preview your form template, go to File > Save As and save it as Config.xml. This will save the schema as well as the default data in an XML format.

3.       Close Preview.

Add a Data Connection to the XML File

We are going to add our new configuration XML as a resource to our template.

4.       Go to the Data tab > Data Connections, and then click Add.

5.       Add a data connection that receives data from an XML document.

6.       Browse to the Config.xml file saved earlier.

7.       Click Next twice leaving the default selections, click Finish and then click Close.

Change the Drop-Down to get Choices from the Secondary Data Source

8.       Modify the data source for the SelectedModule drop-down list box to use the default Modules in the new Config data source:

a.   Right-click on the SelectedModule drop-down and select Drop-Down List Box Properties.

b.   Change List box choices to Get choices from an external data source. The Data source will default to the Config data connection you created earlier:

c.   Click on the icon next to Entries, select the Module repeating group under Modules and then click OK.

d.   Leave Value set to my:ModuleNo, but change Display name to my:ModuleName, and then click OK.

Use InfoPath Functions to Get Row Numbers

In this section, we will use the built-in InfoPath functions count and preceding-sibling to create row counts for the Module and the Question repeating groups. We will use these row counts as default values to the ModuleNo and the QuestionNo fields. We will then use a formula to auto-increment the fields as we add new rows to the repeating groups.

9.       Set the default value of the ModuleNo field.

a.   In the Fields taskpane, double-click on the ModuleNo field to display its properties.

b.   Under Default Value, click on the fx button next to the Value box.

c.   Enter the formula: count(../preceding-sibling::my:Module) + 1

d.   Click Verify Formula and you'll see this:

e.   Click OK twice.

10.    Set the default value of the QuestionNo field.

a.   In the Fields taskpane, double-click on the QuestionNo field to display its properties.

b.   Under Default Value, click on the fx button next to the Value box.

c.   Enter the formula: count(../preceding-sibling::my:Question) + 1

Change the Default Values to Pull Data from the Secondary Data Source

In the Fields taskpane, if you double-click on the ModuleName field of the main data source to see its properties, you will see the the default value you've manually added from Lab 1. We will change this value to pull from the configuration file instead.

11.    Click on the fx button and then click Insert Field or Group...

12.    Switch to the Config (Secondary) data source and follow the series of steps shown in the image below to select and filter the ModuleName field.

13.    Click OK four times.

If you check the Edit XPath (advanced) box, you'll see the XPath xdXDocument:GetDOM("Config")/my:QuizForm/my:Modules/my:Module/my:ModuleName[../my:ModuleNo = current()/../my:ModuleNo]

This means that the control will show the value of the Config ModuleName field  filtered so that the Config ModuleNo is the same as the main ModuleNo which is being set by the incrementing Module row count we've set earlier. Everything inside the square braces ( [ ] ) is an XPath filter. Basically, we are telling InfoPath we'd like the ModuleName from the Config and then providing further information to make sure the correct module name is selected.

14.    Click OK twice to close the dialogs.

Next, we will change the question texts' default values to pull from the configuration file using the same technique.

15.    Double-click on the QuestionText field of the main data source to see its properties, you will see the the default value you've manually added from Lab 1.

16.    Click on the fx button and change it to point to the secondary data source:

a.   Click Insert Field or Group...

b.   Switch to the Config (Secondary) data source and select QuestionText.

c.   Click Filter Data... > Add.

d.   Select QuestionNo in the first drop-down.

e.   Choose Select a field or group... in the third drop-down.

f.   Switch to the Main data source and select QuestionNo.

g.   Click OK once.

17.    We will add another filter condition in order to get only the question set for the corresponding module.

a.   Back in the Specify Filter Conditions dialog, click on the button labeled And.

b.   Choose Select a field or group... in the first drop-down.

c.   Select the ModuleNo field from the Config data source and click OK.

d.   Choose Select a field or group... in the third drop-down.

e.   Switch to the Main data source and select ModuleNo.

f.   Click OK four times and then check the Edit XPath (advanced) box to see the formula

The formula used is: xdXDocument:GetDOM("Config")/my:QuizForm/my:Modules/my:Module/my:Question/my:QuestionText[../my:QuestionNo = current()/../my:QuestionNo and ../../my:ModuleNo = current()/../../my:ModuleNo]

Here again, we are telling InfoPath we'd like the QuestionText from the Config and then added filters to make sure the correct question for the corresponding module is selected.

Add More Default Rows

At this point, you would want to add a couple more default rows for your repeating groups so that you'll have placeholders for new items that are added via the Config.xml.

18.    Go to the Data tab > Default Values.

19.     Expand Modules >  Module (last repeating group), and then select the last Question repeating node.

20.    Click on the down-arrow key next to node, and then select Add another Question below.

21.    Keep adding until you have about 10 repeating Question nodes. Note that if you ever exceed this, i.e. you modify Config.xml to add more than 10 questions for a module - you will need to do this step again and add placeholders.

22.    When done, select the last Module repeating node and also add more until you have about 5.

23.    Click OK when done.

When you preview your form template, you will see almost no difference to the template you've created for Lab 1 - only now, we are not using default data; instead, our data is retrieved from a secondary XML data source.

Comments

 

Twitter Trackbacks for Free Training: Data-Driven Design - Using XML Files to Configure Other Forms (Lab 2) - Mel Balsamo [infopathdev.com] on Topsy.com said:

Pingback from  Twitter Trackbacks for                 Free Training: Data-Driven Design - Using XML Files to Configure Other Forms (Lab 2) - Mel Balsamo         [infopathdev.com]        on Topsy.com

December 13, 2010 8:25 PM
Copyright © 2003-2014 Qdabra Software. All rights reserved.
View our Terms of Use.