Dynamically fill a drop-down list box in a browser form using another InfoPath form as a data source in SharePoint
Use the FileLocation property of a FileQueryConnection in InfoPath to dynamically populate an InfoPath drop-down list box using C# and different XML files when populating the drop-down list box in an InfoPath browser form in SharePoint.
You have a drop-down list box on an InfoPath browser form in SharePoint, and you would like to dynamically change items of the list by switching between XML files at runtime.
Use the FileLocation property of a FileQueryConnection in InfoPath to switch and point to different XML files when dynamically populating the InfoPath drop-down list box on the form.
InfoPath forms are XML files and XML files can be used as receive data connections in InfoPath. So InfoPath forms can be used as receive data connections in other InfoPath forms. Since InfoPath forms can be stored in SharePoint form libraries, they can easily be accessed by InfoPath forms hosted by InfoPath Forms Services.
Drop-down list boxes can be bound to data connections (XML file, database, SharePoint list, or web service). Once a drop-down list box has been bound to a data connection, the XML schema that defines the items in the drop-down list box cannot be changed at runtime. So all data that is returned by any receive data connection bound to the drop-down list box must fit the pre-defined XML schema for the items of the drop-down list box or must be altered to fit this XML schema.
To overcome this limitation, you can bind a drop-down list box to an InfoPath form. This enables the drop-down list box to use the XML schema of the InfoPath form template that the InfoPath form is based on, so that any InfoPath form that is created from this form template can be used to dynamically populate the InfoPath drop-down list box with items.
So by binding a drop-down list box to an InfoPath form, you can use different instances of that InfoPath form to dynamically change the items in the drop-down list box without changing the XML structure of the items for the drop-down list box. You can then use the FileLocation property of a FileQueryConnection in InfoPath that defined the connection to the XML of the initial InfoPath form bound to the drop-down list box to point to other InfoPath forms that are based on the same InfoPath form template as the first InfoPath form to dynamically populate the InfoPath drop-down list box with items.
You dynamically populate an InfoPath drop-down list box using C# as follows:
- Design an InfoPath form template as shown in figure 1. This form template will be used
to create InfoPath forms that will dynamically populate the InfoPath drop-down list box using C# code.
Figure 1. The InfoPath form template in design mode.
with a Main data source that resembles the following figure:
Figure 2. The Main data source of the InfoPath form template.
- Publish the form template to SharePoint and create a new form library for it.
- Go to SharePoint and fill out two instances of the form. For example, create a new
form containing a list of Fruits in the Repeating Table of the
form and another form containing a list of Vegetables in the Repeating Table of the
Figure 3. Two new InfoPath forms in the SharePoint form library.
Figure 4. Contents of the Fruits InfoPath form.
- Download a copy of the Fruits form and save the downloaded XML file to your Desktop.
Figure 5. Downloading a copy of the Fruits InfoPath form from the SharePoint Form Library.
- Design a new InfoPath browser-compatible form template with two Drop-Down List Box
controls named selectionDD and fruitOrVegetable and a Main data source
that resembles the following figure:
Figure 6. The Main data source of the InfoPath form template.
- Create a Receive data connection to a SharePoint library or list, select the form library you created in step 2, and make sure to check the Title field from the list of fields for the SharePoint library. Accept all the default settings in the Data Connection Wizard.
- Bind the selectionDD to the data connection you just created and use the Title field for both the Value and Display name of the drop-down list box.
- Create a new Receive data connection to an XML document, browse to the XML file for the Fruits form that you saved in step 4, select Access the data from the specified location, and do not check Store a copy of the data for offline use. Rename the data connection to Items and clear the check box for Automatically retrieve data when form is opened.
- Bind the fruitOrVegetable drop-down list box to the Items data connection
you just created.
Figure 7. List box entries on the Properties dialog box of the InfoPath drop-down list box.
- Add a Changed event handler to the selectionDD field and a Loading event handler to the form template.
- Put the following C# code in both the Changed and Loading event handlers:
// Get the name of the InfoPath form to use from the selectionDD drop-down list boxwhere <Server_Name> should be replaced by the name of your SharePoint server and <Form_Library_Name> with the name of the form library you created in step 2.
string selectedXMLFile = MainDataSource.CreateNavigator().SelectSingleNode(
// Retrieve the data connection bound to the fruitOrVegetable drop-down list box
FileQueryConnection con = (FileQueryConnection)DataConnections["Items"];
// Change the location of the data connection to point to the XML file selected in selectionDD
con.FileLocation = @"http://<Server_Name>/<Form_Library_Name>/" + selectedXMLFile
// Query the data connection to populate the InfoPath drop-down list box with items
- Double-click the selectionDD field to open its Properties dialog box.
- On the Drop-Down List Box Properties dialog box, click the Browser forms tab.
- On the Browser forms tab, choose Always for the Postback settings.
- Publish the form template to a SharePoint Server running Forms Services. Since the form template contains managed code, you will have to perform an administrator-approved form template deployment.
You should now be able to select an item from the first drop-down list box and have the second drop-down list box dynamically filled with items from the XML file selected from the first drop-down list box and which was stored as an InfoPath form in a SharePoint form library.
Copyright: This article may not be used on web sites (whether personal or otherwise), copied, disseminated, altered, printed, published, broadcasted, or reproduced in any way without an expressed written consent of S.Y.M. Wong-A-Ton. The techniques demonstrated in this article may be used within any Microsoft InfoPath project. This article is provided without any warranties. Copyright for this article is non-transferrable and remains with the author, S.Y.M. Wong-A-Ton.
Related InfoPath Articles:
- How to add a data connection to a SharePoint list or library in InfoPath
- Programmatically add items to a drop-down list box in InfoPath 2007
- Use an Access table as a secondary data source in a browser-compatible InfoPath form
- Auto-populate a drop-down list box with past and future dates
- Dates in an InfoPath repeating table without weekends