How to close an InfoPath form programmatically

Learn how to in InfoPath close a web-based form programmatically by working around a few limitations of InfoPath where rules and code are concerned.

ADVERTISEMENTS

A web-based form can only be closed with a rule

You can only close an InfoPath form (web-based) if you create a rule with an action that says:

Close the form

In addition, such a rule can only be set on a button control, which means that you will not be able to close an InfoPath form when a user changes the value of a field, but only when a user clicks a button.

Rules run before code

A design feature of InfoPath is that rules run before code, which means that even if you write code in a button that has the Close the form action set on it, the code will execute after the Close the form rule runs.

So you won’t be able to close the InfoPath form programmatically by writing code behind a button that has a rule set on it.

When code runs before rules

There is an exception to the rules run before code feature, and that is when you use a form’s Submit Options.

On the Submit Options dialog box of an InfoPath form template, you can set a Close the form rule, which will run after the form is submitted. This way you can write code in the FormEvents_Submit event handler to programmatically submit an InfoPath form and supposedly close the InfoPath form programmatically after submitting it.

Using a custom Submit button

By default, the FormEvents_Submit event handler is executed whenever a user clicks the Submit button on the toolbar of a web-based InfoPath form running in InfoPath Forms Services on SharePoint.

But you can also create a button that has a Submit action and hook it up to the FormEvents_Submit event handler.

Submit to multiple data connections and close the form

The FormEvents_Submit event handler allows you to write code to execute more than one data connection (e.g., to send data through email in InfoPath, to submit an InfoPath form to a SharePoint library, to submit data from an InfoPath form to a web service, etc.) before closing the InfoPath form.

A good example of this is if you want to programmatically submit an InfoPath form to a SharePoint form library, send an email, and then close the form.

 
 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