How to submit rows of a repeating table in InfoPath to a web service

Learn how you can create a .NET web service that accepts data from the rows of a repeating table on an InfoPath form.

ADVERTISEMENTS

Problem

You have an InfoPath repeating table on a form and you want to submit the data from all of the rows in the repeating table to a .NET web service.

Solution

Create a .NET web service that accepts a parameter that can be serialized as an array of XML elements.

Discussion

You can achieve this functionality as follows:

  1. In InfoPath, create a new form template.
  2. Add a Repeating Table control with 3 columns to the InfoPath form template. By default the repeating table will contain a non-repeating group node named group1, a repeating group node named group2, and 3 fields named field1, field2, and field3.
    The default structure of a repeating table in InfoPath
    Figure 1. The default structure of a repeating table in InfoPath.

    The repeating table shown above can be seen as an array of group2 nodes. This logic will be used by the web service to accept the XML data of the repeating table that is passed from the InfoPath form, and serialize this XML into an array of group2 items.

  3. In Visual Studio 2005, create a new Web Site project.
  4. Select the ASP.NET Web Service Visual Studio template, and name the web service SubmitRepeatingTableInfoPath.
  5. Add a new Class to the App_Code folder and name it RepeatingTable.
  6. Add the following code to the RepeatingTable class:

    [System.Xml.Serialization.XmlRootAttribute(
    Namespace = "http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-02-23T05:14:24",
    IsNullable = false)]
    public class RepeatingTable
    {
    [System.Xml.Serialization.XmlArray("group1")]
    public group2[] rows;
    }

    public class group2
    {
    public string field1;
    public string field2;
    public string field3;
    }

    Note: You must replace the Namespace parameter of XmlRootAttribute with the namespace for your own InfoPath form template. To find this namespace, in InfoPath, on the File menu, click Save As Source Files, and select a folder where to extract the files contained in the form template. Go to the folder where you extracted the files, open the sampledata.xml file in Notepad, copy the namespace for the my namespace prefix and paste it into the class file in Visual Studio.

  7. Delete the HelloWorld web method that Visual Studio adds by default to web service projects and add the following SubmitRepeatingTable web method to the web service:

    [WebMethod]
    public void SubmitRepeatingTable(
    RepeatingTable myRepTable)
    {
    for (int i = 0; i < myRepTable.rows.Length; i++)
    {
    string field1 = myRepTable.rows[i].field1;
    string field2 = myRepTable.rows[i].field2;
    string field3 = myRepTable.rows[i].field3;
    }
    }

  8. In InfoPath, on the Tools menu, click Data Connections.
  9. On the Data Connections dialog box, click Add.
  10. On the Data Connection Wizard, select Create a new connection to, select Submit data, and click Next.
  11. On the Data Connection Wizard, select To a Web service, and click Next.
  12. On the Data Connection Wizard, type in the location for your web service, and click Next.
  13. On the Data Connection Wizard, select SubmitRepeatingTable from the Select an operation list, and click Next.
  14. On the Data Connection Wizard, click the button behind the Field or group field.
  15. On the Select a Field or Group dialog box, expand the nodes, select the group1 node, and click OK.
  16. On the Data Connection Wizard, select XML subtree, including selected element from the Include drop-down list box, and click Next.

    Data Connection Wizard in InfoPath to select the repeating table node to submit to the web service
    Figure 2. Data Connection Wizard in InfoPath to select the repeating table node to submit to the web service.

  17. On the Data Connection Wizard, click Finish.
  18. On the Data Connections dialog box, click Close.
  19. On the Tools menu, click Submit Options.
  20. On the Submit Options dialog box, select Allow users to submit this form, select Web service from the Send form data to a single destination drop-down list box, select the web service submit data connection you created from the Choose a data connection for submit drop-down list box, and click OK.

You should now be able to preview the InfoPath form, enter data in the repeating table, and click the Submit button to submit the data of the repeating table to the web service.

 
 Subscribe for updates 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 InfoPath Articles:

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