Programmatically adding rows to repeating sections - Jimmy Rishe
in

InfoPath Dev

Jimmy Rishe

Programmatically adding rows to repeating sections

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:

  1. Right-click the control (you can click the little tab at the bottom-left corner of the repeating section or table)
  2. Select Repeating Table Properties... or Repeating Section Properties... (at the bottom of the context menu)
  3. 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.

Comments

 

duncan.hope said:

Very handy, thanks.

March 27, 2009 4:10 AM
 

NateHarris said:

OK, I am not *that* new to Infopath, but I am not a coder either.  I have a form that I want to query a list, and add a line for each entry in the list I query.  Basically it's a list of Names, and I want to add a new Repeating Item for each name in the list.  But I guess I am just too thick and cannot figure out how to use qRules to do this.  How do i call the Command and set the value to perform this ExecuteAction function?  I thought I would just use a Button to perform the task, with the following code added under Rules/Set a Fields Value, then choose Command from the 2ndary data source, with a value of "ExecuteAction /action=XCollectionInsert /xmltoedit=group4_1".  This doesn't work, so any pointers would be VERY much appreciated.

[email protected]

April 30, 2009 12:55 PM
 

tfiema said:

OK

But how would you insert a prepopulated row into a repeating table using this?

in other words , how would you insert a row with values instead of blanks?

Thanks

May 8, 2009 11:39 AM
 

tfiema said:

To explain the non-browser form issue: The ExecuteAction method is only available in the client side Microsoft.Office.Infopath.dll (the 59KB one).

The server side one (47KB), the one referenced by forms services doesn't have this method. Meaning browser based forms cannot use this code. For browser based forms you'll need to clone a row, or do an appendChild. Either way thise seem to work only if there is already a blank row in the table (of course you need one to clone).

May 11, 2009 6:22 AM
 

NashParrot said:

I'm using this strategy for repeating sections, very succesfully.  It is based on a multi-select list.  I need to assign a value contained in that section (row) but don't know how to select the newly created section in order to get to the field.  Any ideas?

October 29, 2009 5:26 AM
 

SPowell said:

I LOVE YOU.

THANK. YOU.

May 28, 2012 1:10 PM
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.