Users on the InfoPathDev forum frequently ask how they can add rows to repeating groups from code. There is a relatively simple way to do this, but it has a few limitations. I may write another blog post later that describes a more general approach that is trickier but does not have these limitations, but for now, this option is available for forms that meet the following criteria:
-
The form must not be browser-enabled.
-
The repeating group must be represented in a repeating section or repeating table in the form.
-
The view containing the repeating section/table must be the currently displayed view when the row is added.
-
The repeating group must not be inside another repeating group, or be recursive.
If your form meets all of these requirements, please read on.
XmlToEdit
Before proceeding, you must understand a bit about the XmlToEdit value. Certain controls (including repeating sections and tables) have a property called XmlToEdit, which has a unique value for each different control. You can find this value with the following steps:
-
Right-click the control (you can click the little tab at the bottom-left corner of the repeating section or table)
-
Select Repeating Table Properties... or Repeating Section Properties... (at the bottom of the context menu)
-
Click the Advanced tab. The XmlToEdit value should be displayed towards the bottom of the dialog box.
Throughout this blog post I will be using the text XmlToEdit as a placeholder for this value. Make sure to replace this text with the actual XmlToEdit value of your control.
With that out of the way, let's look at the actual code. It is a single line in all six language models that InfoPath supports and I will show you that code for each language.
C# 2007
CurrentView.ExecuteAction(ActionType.XCollectionInsert, "XmlToEdit");
Visual Basic 2007
CurrentView.ExecuteAction(ActionType.XCollectionInsert, "XmlToEdit")
C# 2003
thisXDocument.View.ExecuteAction("xCollection::insert", "XmlToEdit");
Visual Basic 2003
thisXDocument.View.ExecuteAction("xCollection::insert", "XmlToEdit")
JScript
XDocument.View.ExecuteAction("xCollection::insert", "XmlToEdit");
VBScript
XDocument.View.ExecuteAction "xCollection::insert", "XmlToEdit"
qRules 1.5 (codeless forms)
Forms with qRules 1.5 (free trial on Qdabra.com) can also use a similar operation to add rows to a repeating table. In this case, use the command:
ExecuteAction /action=XCollectionInsert /xmltoedit=XmlToEdit
For more information on using qRules and specifying commands, please see the qRules documentation.