InfoPath Fill or empty a repeating table in browser-based form by using code-behind

InfoPath is a powerful tool which can be customized by using rules or custom code behind. At one of my projects it was necessary to display some additional information from a database dependent on which checkbox the user clicked / checked. Another request to this was that the form has to be browser-enabled cause it should be displayed in a SharePoint environment.

So i have 3 Checkboxes: A, B, C. And i have a section with a repeating table which is structured like this:

I use the onchange Event of each Checkbox which calls the function. Now let’s see how we can fill this repeating table with data:

XPathNavigator node = navForm.SelectSingleNode("/my:meineFelder/my:Section/my:RepeatingTable", NS).Clone();
foreach (DataRow row in DataSet.Tables[0].Rows)
node.SelectSingleNode("/my:meineFelder/my:Section/my:RepeatingTable/my:FieldA", NS).SetValue(row["Field1"].ToString());
node.SelectSingleNode("/my:meineFelder/my:Section/my:RepeatingTable/my:FieldB", NS).SetValue(row["Field2"].ToString());
node.SelectSingleNode("/my:meineFelder/my:Section/my:RepeatingTable/my:FieldC", NS).SetValue(row["Field3"].ToString());
node.SelectSingleNode("/my:meineFelder/my:Section/my:RepeatingTable/my:FieldD", NS).SetValue(row["Field4"].ToString());
node.SelectSingleNode("/my:meineFelder/my:Section/my:RepeatingTable/my:FieldE", NS).SetValue(row["Field5"].ToString());
navForm.SelectSingleNode("/my:meineFelder/my:Section", NS).AppendChild(node);


At first, i clone the node. After that i fill the fields of this clone with my data of my dataset. Now i can append this node to the current repeating table. After finishing the foreach i delete the node. After that you can see your data in the repeating table like this:


But what happens if the user clicks now on the checkbox B? Normally it appends the rows to the current repeating table. So we have to delete all rows first. For that i use a small trick.
I do not delete each row, instead i delete the whole repeating table and create a new one. In order to do that i save at formEvents Loading the empty repeating table into a member variable from which i can create a new repeating table.

So first add a member variable to your code:

private object initialData
return FormState["initialData"];
FormState["initialData"] = value;

After that i add in the FormEvents_Loading

this.initialData = navForm.SelectSingleNode("/my:meineFelder/my:Section", NS).OuterXml;

Now the interesting part begins: How to use this member variable for creating a new empty repeating table:

if (initialData != null)
navForm.SelectSingleNode("/my:meineFelder/my:Section", NS).ReplaceSelf(initialData.ToString());

That’s it, just use the ReplaceSelf funtion of infopath. Hope this helps you.


The article or information provided here represents completely my own personal view & thought. It is recommended to test the content or scripts of the site in the lab, before making use in the production environment & use it completely at your own risk. The articles, scripts, suggestions or tricks published on the site are provided AS-IS with no warranties or guarantees and confers no rights.

About Karsten Schneider 308 Articles
Consultant for Microsoft 365 Applications with a strong focus in Teams, SharePoint Online, OneDrive for Business as well as PowerPlatform with PowerApps, Flow and PowerBI. I provide Workshops for Governance & Security in Office 365 and Development of Solutions in the area of Collaboration and Teamwork based on Microsoft 365 and Azure Cloud Solutions. In his free time he tries to collect tipps and worthy experience in this blog.

Be the first to comment

Leave a Reply

Your email address will not be published.