Trick: Clear or reset fields in InfoPath using only 3 lines of code

Learn how you can use just 3 lines of code and a 'special setup' in an InfoPath form to be able to create a reset button that can clear or reset fields or a subset of fields on an InfoPath form, whether it is browser-enabled or not.

ADVERTISEMENTS

While I’ve written about how to reset 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 in InfoPath.

These are the options you have for resetting fields in InfoPath:

  1. 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.
  2. 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.
  3. 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 fields on the entire 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.

InfoPath form template with a Reset button
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.

The Main data source of an InfoPath form template with a Reset button
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:

  1. A member variable to the FormCode.cs file:
    private string initialData;
  2. The following code in the Loading event:
    initialData = MainDataSource.CreateNavigator().
    SelectSingleNode("//my:container", NamespaceManager).OuterXml;
  3. 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:

  1. Create a property that makes use of the FormState dictionary:
    private object initialData
    {
    get
    {
    return FormState["initialData"];
    }
    set
    {
    FormState["initialData"] = value;
    }
    }
  2. Add the following code in the Loading event:
    XPathNavigator root = MainDataSource.CreateNavigator();
    initialData = root.SelectSingleNode(
    "//my:container", NamespaceManager).OuterXml;
  3. 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.

 
 Subscribe via RSS or Email

Copyright: This article may not be used on web sites (whether personal or otherwise), copied, disseminated, altered, printed, published, broadcasted, or reproduced in any way without an expressed written consent of S.Y.M. Wong-A-Ton. The techniques demonstrated in this article may be used within any Microsoft InfoPath project. This article is provided without any warranties. Copyright for this article is non-transferrable and remains with the author, S.Y.M. Wong-A-Ton.

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners
InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013
InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners
InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010
InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers
InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services


Related Posts

ADVERTISEMENTS

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners

InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013

InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners

InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010

InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers

InfoPath 2010 Cookbook 4: 101 Code Recipes for VB Developers

InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services