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.
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.
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.
You can achieve this functionality as follows:
In InfoPath, design a browser-compatible InfoPath form template as shown in Figure 1.
Figure 1. The InfoPath form template in Design mode.
- 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
// Submit the form to SharePoint
DataConnection spConn =
DataConnections["SharePoint Library Submit"];
// Set the properties for the email
string toAddress = root.SelectSingleNode(
EmailSubmitConnection emailConn =
emailConn.Subject.SetStringValue("This subject was set from code");
emailConn.Introduction = "This email was generated by code.";
emailConn.EmailAttachmentType = EmailAttachmentType.None;
// Send the email
// Indicate success
e.CancelableArgs.Cancel = false;
- Save your work and build the code.
- 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.
- Save your work and publish the form template to a SharePoint server running InfoPath Forms Services. Since you wrote code in the InfoPath form template, you will have to perform an administrator-approved form template deployment.
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 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.
Related InfoPath Articles:
- Use an InfoPath form and SharePoint Designer workflow to send an email with attachments
- How to send email from an InfoPath browser form in SharePoint
- How to create a SharePoint Designer workflow that sends an email with a link to an InfoPath browser form
- Video: Send an email notification from SharePoint based on the values in an InfoPath form
- Video: Send email via an InfoPath browser form in SharePoint 2013