While I’ve written about resetting InfoPath form fields in the past, I’d like to recap the options you’ve got available and also present a new solution that requires only 3 lines of code to create a Reset button in InfoPath.
First, let us define what we mean by resetting:
Clearing all fields or a subset of fields on an InfoPath form and restoring the InfoPath form to its original state as when it was opened for the first time.
InfoPath does not come out-of-the-box with a Reset button. So you’ll have to resort to either using Rules or writing code to reset fields.
These are the options you have for resetting fields in InfoPath:
- If you have a limited amount of fields (e.g. less than 10) on an InfoPath form you want to clear or reset, you can use the Set a field’s value action in a rule for each field you want to reset. Limitation: You won’t be able to clear the rows in a repeating table using this technique.
- If you have a limited amount of fields (e.g. less than 10) on an InfoPath form you want to clear or reset, you can write code to set the value of each field you want to reset. In addition, by using code you can also delete all of the rows in a repeating table to clear it.
- If you have many fields (e.g. more than 50) on an InfoPath form, using the first or second method will quickly become cumbersome. It is then best to use the technique I’ll explain next.
Suppose you have the following InfoPath form template with controls and a button control for which you want to write code to reset all of the fields on the InfoPath form.
Note that the repeating table is pre-populated with 3 items and that when you clear the fields, you also want to revert back to the repeating table containing only these 3 items.

Figure 1. InfoPath form template with a Reset button.
The key to using this technique is to place all of the controls you want to clear or reset under one node and then use the ReplaceSelf method to replace that entire node with the XML of that same node you saved when the form was loaded for the first time.
Here I’ve used a node named container as the main or root node for all of the fields I want to clear, in this case, all of the fields on the InfoPath form.

Figure 2. The Main data source of an InfoPath form template with a Reset button.
If you want this solution to work for InfoPath forms that are filled out through the InfoPath client application, you’d have to add:
-
A member variable to the FormCode.cs file:
private string initialData; -
The following code in the Loading event:
initialData = MainDataSource.CreateNavigator().
SelectSingleNode("//my:container", NamespaceManager).OuterXml; -
The following code in the Clicked event of the Reset button:
MainDataSource.CreateNavigator().SelectSingleNode(
"//my:container", NamespaceManager).ReplaceSelf(initialData);
See? Just 3 lines of code, as promised.
If you want this solution to work for InfoPath browser forms that are filled out through Sharepoint’s user interface, you’ll have to modify the code a bit, because member variables are not supported in browser-enabled forms.
You must change the previous code as follows:
-
Create a property that makes use of the FormState dictionary:
private object initialData
{
get
{
return FormState["initialData"];
}
set
{
FormState["initialData"] = value;
}
} -
Add the following code in the Loading event:
XPathNavigator root = MainDataSource.CreateNavigator();
initialData = root.SelectSingleNode(
"//my:container", NamespaceManager).OuterXml;
-
Add the following code in the Clicked event of the Reset button:
XPathNavigator root = MainDataSource.CreateNavigator();
XPathNavigator container = root.SelectSingleNode(
"//my:container", NamespaceManager);
if (initialData != null)
container.ReplaceSelf((string)initialData);
More than 3 lines of code, but still doable, right?
If you want to see this solution in action for an InfoPath browser form in SharePoint, take a look at this short 2-minute Clear and reset fields in InfoPath using code video.

2 Responses to “Trick: Clear or reset fields in InfoPath using only 3 lines of code”
Posted Thursday, 3 September 2009
Excellent code…however, how do I do the above using jScript?
Posted Thursday, 3 September 2009
Hi Tom,
You can use the same concept, but with the following JScript code:
var initialData;function XDocument::OnLoad(eventObj)
{
initialData =
XDocument.DOM.selectSingleNode(
"/my:myFields/my:container").xml;
}
function CTRL3_5::OnClick(eventObj)
{
// Load the initial saved XML into a DomDocument object
var xmlDoc = new ActiveXObject("MSXML2.DomDocument.5.0")
xmlDoc.loadXML(initialData);
// Replace my:container
var parentNode = XDocument.DOM.selectSingleNode("/my:myFields");
var oldChildNode = parentNode.selectSingleNode("my:container");
parentNode.replaceChild(xmlDoc.documentElement, oldChildNode);
}