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:
RepeatingTable
—–FieldA
—–FieldB
—–FieldC
—–FieldD
—–FieldE

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);

}
node.DeleteSelf();


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:

RepeatingTable

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
{
get
{
return FormState["initialData"];
}
set
{
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.

..:: I LIKE SHAREPOINT ::..

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.

Karsten Pohnke About Karsten Pohnke
He is Consultant for SharePoint Solutions for collaboration, communication and business processes. He provides his customers applications based on standard features as well as development or combining the power of several microsoft tools like Dynamics CRM. In his free time he tries to collect tipps and worthy experience in this blog.

Submit comment

Allowed HTML tags: <a href="http://google.com">google</a> <strong>bold</strong> <em>emphasized</em> <code>code</code> <blockquote>
quote
</blockquote>

Please fill in the captcha: * Time limit is exhausted. Please reload CAPTCHA.