Programmatically retrieve the value of another field in the same row of a repeating table in InfoPath 2007

Applies to: 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.

ADVERTISEMENTS

Problem

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.

Solution

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.

Discussion

You can achieve this functionality as follows:

  1. 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.

    Fruits SharePoint list displaying values for the Title and Color fields.
    Figure 1. Fruits SharePoint list displaying values for the Title and Color fields.

  2. In the Data Source task pane, select the secondary data source bound to the SharePoint list.
  3. 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.
  4. Right-click the Title field in the Repeating Table, select Programming and then Changed Event from the context menu that appears.
  5. 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",
    NamespaceManager).Value;

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",
NamespaceManager).OuterXml;

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:

<dfs:Fruits
xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution"
Title="Kiwi" Color="Brown"
/>

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:

../@Color

Note: You can retrieve an attribute by using an @ sign in an XPath expression.

 


Related InfoPath Articles:

 

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.

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners

InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013

InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners

InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010

InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers

InfoPath 2010 Cookbook 4: 101 Code Recipes for VB Developers

InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services