Programmatically submit an InfoPath form to a SharePoint library, send an email, and then close the form

This article shows how you can use the FormEvents_Submit event handler to programmatically execute 2 data connections, one to submit to a SharePoint form library and the other to send an email, when submitting an InfoPath browser form, and then close the form afterwards.

ADVERTISEMENTS

Problem

You have an InfoPath web-based form running on InfoPath Forms Services in SharePoint. You want to be able to submit this form to a SharePoint form library and then use the data in the form to send an email. After submitting the form and sending the email, you want the form to close the InfoPath form programmatically.

Solution

Create a SharePoint Library Submit data connection and an Email Submit data connection and then
programmatically execute the data connections in the FormEvents_Submit event handler of the InfoPath form,
and perform a Close the form action as part of the Submit Options on the InfoPath form.

Discussion

On the Data Source task pane, add a Text Field node named formName under
the myFields node.

On the Tools menu, choose Data Connections.

On the Data Connections dialog box, click Add, and create a new
Submit data connection As an e-mail message. Fill in
any email address in the To field; you will be changing this in code anyway. Accept the default name of Email Submit for the data connection.

On the Data Connections dialog box, click Add, and create a new Submit data connection To a document library on a SharePoint site. Fill in a valid URL to a Document Library. Select the formName node from the Main data source to be the File name for the form. Accept the default name of SharePoint Library Submit for the data connection.

On the Tools menu, choose Submit Options.

On the Submit Options dialog box, select Allow users to submit this form, deselect the
Show the Submit menu item and the Submit toolbar button check box, click Advanced
and select Close the form from the After submit drop-down list box, select
Perform custom action using Code, and click Edit Code.

Figure 2. The Submit Options dialog box in InfoPath.

This will add a FormEvents_Submit event handler to the InfoPath form template.

In Microsoft Visual Studio Tools for Applications, add the following C# code to the FormEvents_Submit event handler:

// Get a reference to the main data source
XPathNavigator root = MainDataSource.CreateNavigator();

// Generate a name for the form to be saved in SharePoint
string formName = Guid.NewGuid().ToString();

// Set the name of the form on the data connection
root.SelectSingleNode(
"//my:formName", NamespaceManager).SetValue(formName);

In InfoPath, double-click the Send button to open its Properties dialog box.

On the Button Properties dialog box, select Submit from the Action
drop-down list box, type Send as the text for the Label, and click OK.
This will "bind" the Send button to the FormEvents_Submit event handler you
created through the Submit Options dialog box in step 7. Alternatively, you can use the Submit Options
button on the Button Properties dialog box to perform step 7.

On the Tools menu, choose Form Options.

On the Form Options dialog box, select the Browser category, deselect the Show toolbar at top of form and the Show toolbar at bottom of form check boxes, and click OK.

You should now have a fully functional form, so that when you fill in an email address in the Send To field and
click the Send button, the form will be submitted to the SharePoint form library, and then an email will
be send to the email address you entered. You will get a confirmation that the form was successfully submitted and when
you click OK, the form will automatically close.

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. 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.