Hard coding values in InfoPath drop down list box is easy but you can also populate it with dynamic data. Data can be from an XML file, a database or even a SharePoint list. This article shows how you can populate the list box with data from a SharePoint list with out writing a line of code.
All About SharePoint is a blog about SharePoint, InfoPath and related technologies. It was started in 2005 and has good readership in many countries. It covers SharePoint 2003, 2007, 2010, 2013, 2016 and Office 365. You can read articles, tips, tutorials and news. It has code samples and complete applications as well.
Wednesday, June 18, 2008
Populating InfoPath Drop down with SharePoint List Data
Friday, June 6, 2008
InfoPath: Reading from a repeating table
I will start with the code first and then explain what's happening.
Code:
XPathNavigator DOM = this.MainDataSource.CreateNavigator();
XPathNodeIterator nodes = DOM.Select("/my:myFields/my:RepeatingGroup", this.NamespaceManager);
XPathNavigator nodesNavigator = nodes.Current;
XPathNodeIterator nodesText = nodesNavigator.SelectDescendants(XPathNodeType.Element, false);
XPathNodeIterator nodesText = nodesNavigator.SelectDescendants(XPathNodeType.Element, false);
string FirstName = "";
string LastName = "";
while (nodesText.MoveNext())
{
if (nodesText.Current.Name == "my:FirstName")
{
FirstName = nodesText.Current.Value.ToString();
}
{
if (nodesText.Current.Name == "my:FirstName")
{
FirstName = nodesText.Current.Value.ToString();
}
nodesText.MoveNext();
if (nodesText.Current.Name == "my:LastName")
{
LastName= nodesText.Current.Value.ToString();
}
{
LastName= nodesText.Current.Value.ToString();
}
}
nodesText = null;
nodesNavigator = null;
nodes = null;
DOM = null;
nodesNavigator = null;
nodes = null;
DOM = null;
Explanation:
It's a very commong scenario. Developers want to read from the repeating table programmatically. The above code just does that. You take the XPath for the repeating group and create a nodes iterator object. This will iterate through all the descendant nodes. If the node type (XPathNodeType) is "Element", the value will be read into a string variable. That's it. Use MoveNext() to move to the next node in the hierarchy. It's as simple as that!
Subscribe to:
Posts (Atom)