Populate InfoPath drop-down programmatically from code
Learn how you can populate an InfoPath dropdown programmatically by binding the drop-down list box to a repeating node in a secondary data source, and then looping through rows of data in another data source to populate the data source that is bound to the drop-down list box.
Problem
You would like to dynamically populate an InfoPath dropdown from code with items from an XML document.
Solution
Bind the InfoPath drop-down list box to a repeating node in either the Main or a secondary InfoPath data source, and then loop through the nodes in the XML document to populate the data source that is bound to the InfoPath drop-down list box with the data from the XML document.
Discussion
An InfoPath drop-down list box consists of:
- A field in which to store the value of the item that is selected from the InfoPath drop-down list box. This is the field that you drag-and-drop onto the InfoPath form and which represents the drop-down list box.
- A repeating node in the Main or a secondary data source, which the InfoPath drop-down list box is bound to, and which contains the items to display in the drop-down list box.
You can populate an InfoPath dropdown programmatically as follows:
- Open Microsoft Office InfoPath 2007, create a new blank form, and add a Drop-Down List Box
to the form (also see: Insert a drop-down list box). Your form should resemble the following figure:
Figure 1. The InfoPath form template in Design mode. Create an XML file with the following content:
<?xml version="1.0" encoding="UTF-8" ?>
<options>
<option><value/><displayname/></option>
<option><value/><displayname/></option>
</options>This XML file will serve as a secondary data source that will contain the items for the InfoPath drop-down list box, so create a Receive data connection to an XML document for this XML file, and bind the Drop-Down List Box to the data connection you created (also see: Working with the data source and data connections). Note: The XML file contains two option nodes instead of one. This is necessary for InfoPath to recognize the option node as a repeating node. One of these nodes will be deleted later through code before populating the drop-down list box. The remaining node will be copied repeatedly to create the items for the drop-down list box.
Figure 2. Binding the drop-down list box to a secondary data source.- Create an XML document and add a Receive data connection for this XML document to the InfoPath form template. You will use this secondary data source to populate the secondary data source that is bound to the drop-down list box, so that the content of this XML document will show up as items in the drop-down list box. Note: You are not required to use an XML document to populate the InfoPath drop-down list box, but could use any other type of data source or file that contains rows of data, which can be loaded into InfoPath, looped through, and used to populate the secondary data source that is bound to the drop-down list box.
Add the following code to the Loading event handler of the form:
C#RemoveFirstItem();
XPathNodeIterator players = DataSources["players"].CreateNavigator().Select(
"//player", NamespaceManager);
foreach (XPathNavigator player in players)
{
string number = player.SelectSingleNode("number", NamespaceManager).Value;
string name = player.SelectSingleNode("name", NamespaceManager).Value;
AddItem(number, name);
}
RemoveFirstItem();
Visual Basic
RemoveFirstItem()
Dim players As XPathNodeIterator = DataSources("players").CreateNavigator().Select( _
"//player", NamespaceManager)
Dim player As XPathNavigator
For Each player In players
Dim number As String = player.SelectSingleNode("number", NamespaceManager).Value
Dim name As String = player.SelectSingleNode("name", NamespaceManager).Value
AddItem(number, name)
Next
RemoveFirstItem()
Add the following code for the RemoveFirstItem and AddItem methods to the form's code. The RemoveFirstItem method removes one of the option nodes from the secondary data source that is bound to the drop-down list box (see note in step 2). The AddItem method adds an item to the secondary data source that is bound to the drop-down list box, thereby filling the value and displayname for each item that will appear in the drop-down list box.
C#private void RemoveFirstItem()
{
XPathNavigator DOM = DataSources["options"].CreateNavigator();
XPathNavigator group1 = DOM.SelectSingleNode("//options", NamespaceManager);
XPathNavigator field1 = DOM.SelectSingleNode("//options/option", NamespaceManager);
field1.DeleteSelf();
}
private void AddItem(string itemId, string itemName)
{
XPathNavigator DOM = DataSources["options"].CreateNavigator();
XPathNavigator group1 = DOM.SelectSingleNode("//options", NamespaceManager);
XPathNavigator field1 = DOM.SelectSingleNode("//options/option", NamespaceManager);
XPathNavigator newNode = field1.Clone();
newNode.SelectSingleNode("value").SetValue(itemId);
newNode.SelectSingleNode("displayname").SetValue(itemName);
group1.AppendChild(newNode);
}Visual Basic
Public Sub RemoveFirstItem()
Dim DOM As XPathNavigator = DataSources("options").CreateNavigator()
Dim group1 As XPathNavigator = DOM.SelectSingleNode( _
"//options", NamespaceManager)
Dim field1 As XPathNavigator = DOM.SelectSingleNode( _
"//options/option", NamespaceManager)
field1.DeleteSelf()
End Sub
Public Sub AddItem(ByVal itemId As String, ByVal itemName As String)
Dim DOM As XPathNavigator = DataSources("options").CreateNavigator()
Dim group1 As XPathNavigator = DOM.SelectSingleNode( _
"//options", NamespaceManager)
Dim field1 As XPathNavigator = DOM.SelectSingleNode( _
"//options/option", NamespaceManager)
Dim newNode As XPathNavigator = field1.Clone()
newNode.SelectSingleNode("value").SetValue(itemId)
newNode.SelectSingleNode("displayname").SetValue(itemName)
group1.AppendChild(newNode)
End Sub- Save your work, build the project, and test the form.
You should now have a fully functional InfoPath form so that once the form loads, the items from the XML document will appear as items in the InfoPath drop-down list box.
Related InfoPath Articles:
- 4 Ways to programmatically add a row to a repeating table in InfoPath
- InfoPath Basics: List control basics in InfoPath
- Dynamically fill a drop-down list box in a browser form using another InfoPath form as a data source
- Auto-fill fields in InfoPath from a drop-down list box selection and secondary data source
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. Usage of 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.



