A Practical Guide to SharePoint 2013

A Practical Guide to SharePoint 2013
A Practical Guide to SharePoint 2013 - Book by Saifullah Shafiq

Saturday, September 8, 2007

Populating multiselect list box in InfoPath 2007 web forms programmatically

Multi select list box that comes with InfoPath 2007 does not work in web forms so you have to create your own control for usage in web forms. The following article shows you how to create a multiselect list box for web forms:
Ok, you have created a multiselect list box and now you can add static values but what if you want to populate this list box programmatically. How will you do that? The following tip shows you how you can populate this list box programmatically:
Please read the article mentioned above before continuing because without reading that article first it won't make sense to you what I am talking.
I will use the same names (for controls) as mentioned in the Microsoft article so that it's easier for you to understand what's happening. 
For your convenience, I will list the control names here:
options: is the group name
option: is the repeating group
selected: is used for the checkbox and contains true/false value
text: is a text box and is used to store the text value
Create a navigator first to navigate the xml nodes.
XPathNavigator DOM = this.MainDataSource.CreateNavigator();

"arrayItems" is an array of items retrieved from the database. Write a function that returns an array containing the items that you want to show in the multi select list box. I have not included that function here. It's a straight forward thing and not related directly to what I am trying to explain.
We will get the items from the array and add them to the list box. We will not touch the checkbox right now. We will just add the text values to the text boxes. By default, there is one row in the multi select list box. You must create new rows dynamically. Check the array length. If the lenth is 10 items, create 10 new rows. Best way is to iterate through the array and create new rows and populate the rows with the items retrieved from the array.
if (arrayItems.Length > 0)
{
       XPathNavigator Item = DOM.SelectSingleNode("/my:myFields/my:options/my:option", this.NamespaceManager);
       XPathNavigator newItemNode = null;
       for (int itemIndex = 0; itemIndex < arrayItems.Length-1; itemIndex++)
       {
              if (Item != null)
              newItemNode = Item.Clone();
              XPathNavigator navText = newItemNode.SelectSingleNode("/my:myFields/my:options/my:option/my:text", this.NamespaceManager);
                  
              navText.SetValue(arrayItems[itemIndex].ToString());
           
              Item.InsertAfter(newItemNode);
              navText = null;
              newItemNode = null;
       }
              
       Item.DeleteSelf();
       Item = null;
              
}
DOM = null;
           
Ok, so here is what I do. I create a clone of the original row and add a value to the text box of this new row.
newItemNode = Item.Clone(); // create a clone
select the text box of the newly created (cloned) row:
XPathNavigator navText = newItemNode.SelectSingleNode("/my:myFields/my:options/my:option/my:text", this.NamespaceManager);
and set it's value with the item retrieved from the array:
navText.SetValue(arrayItems[itemIndex].ToString());

Likewise, if you want to check the checkboxes programmatically, set the value of the control to boolean true or false. True will check the box, and False will uncheck the box. For example, you can check the box like this:
XPathNavigator navCheck = newItemNode.SelectSingleNode("/my:myFields/my:options/my:option/my:selected", this.NamespaceManager); 
navCheck.SetValue("true");
That's it!