Programmatically add items to a drop-down in InfoPath

Steps needed:

1. Add a Repeating group (parent).
2. Add another Repeating group under the above group with same name as dropdown + "row" (thats just the way i am doing it).
3. Add two fields under the second repeating group, dropdown name+ rowvalue and dropdown name + rowdescription.
4. Bind the Original Dropdown to the repeating group.(with rowvalue and rowdescription).

Use AddItem method below to dynamically add the values in dropdown

private void AddItem(string strDropDownName, XPathNavigator root, XPathNavigator parent, XPathNavigator row, int itemId, string itemName)
{
XPathNavigator newNode = row.Clone();

if (itemId == -1)
{
newNode.SelectSingleNode("//my:myFields/" + strDropDownName + "/" + strDropDownName + "Row/" + strDropDownName + "RowValue", NamespaceManager).InnerXml = "";
}
else
newNode.SelectSingleNode("//my:myFields/" + strDropDownName + "/" + strDropDownName + "Row/" + strDropDownName + "RowValue", NamespaceManager).InnerXml = itemId.ToString();
newNode.SelectSingleNode("//my:myFields/" + strDropDownName + "/" + strDropDownName + "Row/" + strDropDownName + "RowDescrip", NamespaceManager).InnerXml = itemName;
parent.AppendChild(newNode);
}


strDropDownName -> Name of the Original Drop-down
root -> RootNavigation
parent -> root.SelectSingleNode("//my:myFields/" + strDropDownName, NamespaceManager); //pointer to parent repeating group

row -> root.SelectSingleNode("//my:myFields/" + strDropDownName + "/" + strDropDownName + "Row", NamespaceManager); // group inside repeating group
Itemid -> Item ID from datasource
ItemName-> Item display name from datasource

0 comments:

Post a Comment

Disclaimer

This is a personal weblog. The opinions expressed here represent my own and not those of my employer or anyone else. Should you have any questions or concerns please e-mail me at sharepointprogrammingblogger@gmail.com .

Copyright (c) 2010 @ myshaepointwork.blogspot.com. All rights are reserved.Do Not Copy.

@ Learning SharePoint.com