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

You can achieve this functionality as follows:

  1. In InfoPath, design a browser-compatible InfoPath form template as shown in Figure 1.

    The InfoPath form template in Design mode.
    Figure 1. The InfoPath form template in Design mode.

  2. On the Data Source task pane, add a Text Field node named formName under the myFields node.
  3. On the Tools menu, choose Data Connections.
  4. 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.
  5. 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.
  6. On the Tools menu, choose Submit Options.
  7. 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.

    The Submit Options dialog box in InfoPath.
    Figure 2. The Submit Options dialog box in InfoPath.

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

  8. 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);

    // Submit the form to SharePoint
    DataConnection spConn =
      DataConnections["SharePoint Library Submit"];
    spConn.Execute();

    // Set the properties for the email
    string toAddress = root.SelectSingleNode(
      "//my:sendTo", NamespaceManager).Value;
    EmailSubmitConnection emailConn =
      (EmailSubmitConnection)DataConnections["Email Submit"];
    emailConn.To.SetStringValue(toAddress);
    emailConn.Subject.SetStringValue("This subject was set from code");
    emailConn.Introduction = "This email was generated by code.";
    emailConn.EmailAttachmentType = EmailAttachmentType.None;

    // Send the email
    emailConn.Execute();

    // Indicate success
    e.CancelableArgs.Cancel = false;

  9. Save your work and build the code.
  10. In InfoPath, double-click the Send button to open its Properties dialog box.
  11. 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.
  12. On the Tools menu, choose Form Options.
  13. 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.
  14. 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.

 


Related InfoPath Articles:

 

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.

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