A Practical Guide to SharePoint 2013

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

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);
             string FirstName = "";
             string LastName = "";
              while (nodesText.MoveNext())
              {
                  if (nodesText.Current.Name == "my:FirstName")
                  {
                         FirstName = nodesText.Current.Value.ToString();
                  }
                  nodesText.MoveNext();
                 if (nodesText.Current.Name == "my:LastName")
                 {
                       LastName= nodesText.Current.Value.ToString();
                 }
        }
         
              nodesText = 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!

1 comment:

  1. Kindly I need your help in this , I have a forms library at SP 2007 with infopath form that has a repeating table , what I need is when a form is submited to SP the event handler on the form libary to read the data from the repeating table at the form and fill it in alist at SP.

    any help is apperciated

    Leanhit@yahoo.com

    Thanks

    ReplyDelete