Thursday, June 3, 2010

Submit InfoPath 2007 with Code (SharePoint list & Email)

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.
  1. In InfoPath, design a browser-compatible InfoPath form template
  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.
  8. This will add a FormEvents_Submit event handler to the InfoPath form template.
  9. 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;

                                                Save your work and build the code.


                                                0 comments:

                                                Post a Comment