Programmatically retrieve the value of another field in the same row of a repeating table in InfoPath 2007
Use the Site property of the XmlEventArgs object in the Changed event of a field in a repeating table that is bound to a SharePoint list to retrieve the value of another field in the same row of the repeating table.
You have a Repeating Table bound to a SharePoint list. You have added a Changed event handler on the Title field of a Repeating Table and would like to retrieve the value of the Color field that is in the same row as the Title field.
Note: A SharePoint list is used here just as an example, but you can use any repeating table that is bound to a repeating node in either the main or a secondary data source with the technique described in this article.
Use the Site property of the XmlEventArgs object that is being passed to the Changed event to retrieve the value of the Color field in the Changed event handler of the Title field.
You can achieve this functionality as follows:
Add a data connection to a SharePoint list. Here we will use a SharePoint list named Fruits, and select its Title and Color fields to be included in the secondary data source.
Figure 1. Fruits SharePoint list displaying values for the Title and Color fields.
- In the Data Source task pane, select the secondary data source bound to the SharePoint list.
- Drag the repeating node for the SharePoint list (here: the Fruits node) to the InfoPath form template, drop it, and select Repeating Table from the context menu that appears.
- Right-click the Title field in the Repeating Table, select Programming and then Changed Event from the context menu that appears.
In Microsoft Visual Studio Tools for Applications, add the following C# code to the Changed event handler of the Title field to retrieve the value of the Color field:
string color = e.Site.SelectSingleNode("../@Color",
Now whenever you make a change to the Title field and then tab away from it, the value of the Color field will be retrieved.
The XPath expression in step 5 navigates first from the Title attribute upwards to the parent node using the .. expression, and then it navigates back downward to the Color attribute using the /@Color expression
To understand how this works, you have to know what the XML of the row in the repeating table looks like. You can use the following C# code to retrieve this XML:
XPathNavigator fruits = DataSources["Fruits"].CreateNavigator();
string xml = fruits.SelectSingleNode("//dfs:Fruits",
The previous code retrieves the XML for the first Fruits node in the secondary data source of the SharePoint list. The resulting XML looks like the following:
From the previous XML code you can see that both Title and Color are attributes of Fruits, and that Fruits is the parent node of both attributes.
The Changed event was added on the Title field, so to retrieve the value of the Color field, all you have to do in XPath is go up to the parent field (Fruits) and then back down to the other child field (Color), which will result in the following XPath expression:
Note: You can retrieve an attribute by using an @ sign in an XPath expression.
Related InfoPath Articles:
- Programmatically retrieve the value of an InfoPath form field using .NET code
- Repeating Tables and Sections in InfoPath
- What is InfoPath repeating table
- Programmatically delete the first row of a repeating table in InfoPath
- Programmatically delete all of the rows of a repeating table in InfoPath
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.