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.

ADVERTISEMENTS

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:

  1. 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.
  2. 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.

Thus populating an InfoPath drop-down list box means populating the data source the drop-down list box has been bound to instead of setting the value of the drop-down list box. The latter will set the selected value on the drop-down list box instead of filling the drop-down list box with items. This is an important concept to understand when using list boxes in InfoPath and wanting to fill them programmatically with items.

You can populate an InfoPath dropdown programmatically as follows:

  1. Open Microsoft Office InfoPath 2007, create a new blank form, and add a Drop-Down List Box to the form. Your form should resemble the following figure:

    The InfoPath form template in Design mode
    Figure 1. The InfoPath form template in Design mode.
  2. 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. 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.

    Binding the drop-down list box to a secondary data source
    Figure 2. Binding the drop-down list box to a secondary data source.

  3. Create a Players 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.
  4. 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()
  5. 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
  6. 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.

 
 Subscribe for updates via RSS or Email

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.

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners
InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013
InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners
InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010
InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers
InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services


Related InfoPath Articles:

ADVERTISEMENTS

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners

InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013

InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners

InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010

InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers

InfoPath 2010 Cookbook 4: 101 Code Recipes for VB Developers

InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services