Populate InfoPath Dropdown list programmatically

–          Open InfoPath 2010 form in design mode
–          Create a XML document, which will work like a template for Dropdown list.

<?xml version="1.0" encoding="UTF-8" ?>
<options>
<option><value/><displayname/></option>
<option><value/><displayname/></option>
</options>

–          Create a new data connection which will point to XML document 14image1     Click on “Manage Data Connections…”

Click on “Add” that will bring you a Wizard for data connection

We are intended to get some data for Dropdown list, so we will select “Receive Data” option.

We have created a XML document, so will select “XML document” option.

We will use our XML document as a resource file.

Click on “Add” & it will ask you for a XML file.

Give proper name to the data connection & finish the wizard.         You can see the new data connection available in “Fields” window, select that new data connection & you can see the structure of the data connection fields.

Now it’s time to bind the data connection fields to the Dropdown list, so go to its properties.

Select the data source as our newly created data connection.   Select entries as our repeating group (i.e. option).

Select appropriate value field & display name field. Now, it’s time for some programming, so first select the “Language” option under Developer tab of Ribbon Bar. Select the programming language & the location of the Project. Now, select the “Loading Event” under Developer tab of Ribbon Bar & it will open up a Visual Studio with a project.
using Microsoft.Office.InfoPath;
using System;
using System.Xml;
using System.Xml.XPath;

namespace Form1
 {
 public partial class FormCode
 {
 public void InternalStartup()
 {
 EventManager.FormEvents.Loading += new LoadingEventHandler(FormEvents_Loading);
 }
public void FormEvents_Loading(object sender, LoadingEventArgs e)
 {
 LoadCountries();
 }
private void LoadCountries()
 {
 RemoveFirstItem(); //Remove extra Item from top
AddItem("IN", "India");
 AddItem("US","United States of America");
 AddItem("UK", "United Kingdom");
RemoveFirstItem(); //Remove extra Item from top.
 }
private void RemoveFirstItem()
 {
 // get the XML dom from the datasource
 XPathNavigator DOM = DataSources["myXmlTemplate"].CreateNavigator();

// get the repeating entity (options) from XML dom
XPathNavigator group = DOM.SelectSingleNode(“//options”, NamespaceManager);

// get the single entity (option) from XML dom
XPathNavigator field = DOM.SelectSingleNode(“//options/option”, NamespaceManager);

// delete entity
field.DeleteSelf();
}

private void AddItem(string itemId, string itemName)
{
// Get the XML dom from the datasource
XPathNavigator DOM = DataSources[“myXmlTemplate”].CreateNavigator();

// Get the repeating entity (options) from XML dom
XPathNavigator group = DOM.SelectSingleNode(“//options”, NamespaceManager);

// get the single entity (option) from XML dom
XPathNavigator field = DOM.SelectSingleNode(“//options/option”, NamespaceManager);

// make a clone of the first entity in XML dom & set new values
XPathNavigator newNode = field.Clone();
newNode.SelectSingleNode(“value”).SetValue(itemId);
newNode.SelectSingleNode(“displayname”).SetValue(itemName);

// append the new item in repeating entity (options)
group.AppendChild(newNode);
}
}
}

–          Finally, you will get the result

 

1 COMMENTS

  1. Anonymous
    February 23, 2013 06:48 Reply

    excellent post. Ne’er knew this, thanks for letting me know.

Leave a reply