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.

ADVERTISEMENTS

Problem

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.

Solution

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.

Discussion

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:

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

    with a Main data source that resembles the following figure:

    The Main data source of the InfoPath form template
    Figure 2. The Main data source of the InfoPath form template.
  2. Publish the form template to SharePoint and create a new form library for it.
  3. 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 form.

    Two new InfoPath forms in the SharePoint form library
    Figure 3. Two new InfoPath forms in the SharePoint form library.

    Contents of the Fruits InfoPath form
    Figure 4. Contents of the Fruits InfoPath form.
  4. Download a copy of the Fruits form and save the downloaded XML file to your Desktop.
    Downloading a copy of the Fruits InfoPath form from the SharePoint Form Library
    Figure 5. Downloading a copy of the Fruits InfoPath form from the SharePoint Form Library.
  5. 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:

    The Main data source of the InfoPath form template
    Figure 6. The Main data source of the InfoPath form template.
  6. 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.
  7. 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.
  8. 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.
  9. Bind the fruitOrVegetable drop-down list box to the Items data connection you just created.

    List box entries on the Properties dialog box of the fruitOrVegetable drop-down list box
    Figure 7. List box entries on the Properties dialog box of the InfoPath drop-down list box.
  10. Add a Changed event handler to the selectionDD field and a Loading event handler to the form template.
  11. 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 box
    string selectedXMLFile = MainDataSource.CreateNavigator().SelectSingleNode(
        "//my:selectionDD", NamespaceManager).Value;

    // 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
        + "?noredirect=true";

    // Query the data connection to populate the InfoPath drop-down list box with items
    con.Execute();
    where <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.
  12. Double-click the selectionDD field to open its Properties dialog box.
  13. On the Drop-Down List Box Properties dialog box, click the Browser forms tab.
  14. On the Browser forms tab, choose Always for the Postback settings.
  15. 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.

 


Related InfoPath Articles:

 

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

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