You cannot automatically submit structured XML data to a Web service in InfoPath 2003

Article translations Article translations
Article ID: 826989 - View products that this article applies to.
This article has been archived. It is offered "as is" and will no longer be updated.
Expand all | Collapse all

On This Page

Symptoms

When you submit information from an InfoPath form to a Web service by using the built-in InfoPath submit functionality, you may experience one of the following symptoms:
  • You may receive an error message. The error message that you receive varies according to the information that is being submitted and to the coding of the method that is being invoked on the Web service.
  • You may notice that unexpected data is submitted to the Web service.
This article also discusses the following:
  • How InfoPath submits data to a Web service.
  • Two methods for submitting the contents of an InfoPath form to a Web service.

Cause

When InfoPath submits data to a Web service, it submits the contents of the XML node that was selected as a parameter, but it does not submit the XML node itself. This generally works well. For example, you have a Web service method like the following:
[WebMethod]
   public void SendSampleString( string sampleString )
   {
      //Do something interesting with the sample string.
   } 
If you submit the following XML element to this Web service method, the sampleString parameter is filled with "Hello, World!" (as you expect).
<sampleElement>"Hello, World!"</sampleElement>
Unfortunately, this behavior can make it difficult to submit XML nodes or the whole InfoPath form to a Web service. For example, you have a Web service method like the following:
[WebMethod]
   public string SendXMLElement( System.Xml.XmlElement theElement )
   {
      //Report how many children the submitted node had.
      return "Node with " + theElement.ChildNodes.Count + " children submitted.";
   }
If you submit the example XML element to this method by using the built-in InfoPath submit functionality, the contents of the theElement parameter generate an error. This error occurs because only the contents of the XML element ("Hello, World!"), are sent to the Web service method. Because the string is not a valid XML element, the .NET Framework generates an error; the error message appears in InfoPath.

A similar problem occurs if you try to submit the whole InfoPath form to the example Web service method. In this case, InfoPath submits all the child nodes of the InfoPath form, but does not submit the root node. This results in multiple top-level XML nodes being submitted. Because this is also not valid XML, the .NET Framework selects the first top-level XML node and uses it as the theElement parameter. All the other nodes are ignored, and this gives the appearance that they were not submitted.

Resolution

This problem is corrected in Microsoft Office 2003 Service Pack 1. To resolve this problem, obtain the latest service pack for Microsoft Office 2003.

For additional information, click the following article number to view the article in the Microsoft Knowledge Base:
870924 How to obtain the latest service pack for Office 2003

Workaround

To work around this problem, write a custom submit action in script. You can use either of the following methods:
  • Use a secondary data source to perform the submit operation.
  • Write code that creates a custom SOAP message and that sends it to the Web service.

Use a Secondary Data source

  1. On the Tools menu, click Secondary Data Sources.
  2. In the Secondary Data Sources dialog box, click Add.
  3. On the first page of the Data Source Setup Wizard, click Web Service, and then click Next.
  4. Enter the location of the Web service that you want to submit, and then click Next.
  5. Select the Web service method that you want to use, and then click Next.
  6. When InfoPath prompts you to select the field or group from the form that will be used to fill the parameters of the Web service method, leave the parameters blank, and then click Next.
  7. Click to clear the "Connect to this secondary data source when the form is opened" check box, click Finish, and then click OK to close the Secondary Data Sources dialog box.
  8. On the Tools menu, click Submitting Forms.
  9. In the Submitting Forms dialog box, click Enable Submit.
  10. In the Submit list, click Submit using Custom Script, and then click OK to open Microsoft Script Editor.
  11. In the XDocument::OnSubmitRequest method, use script like the following script to submit the whole form.

    Note You can also submit individual elements in the form.
    try{
          //Get a reference to the SendXMLElement secondary data source.
          var objSendXMLElement = XDocument.GetDOM("SendXMLElement");
          objSendXMLElement.setProperty( "SelectionNamespaces",
             'xmlns:s1="http://joelallxp.microsoft.com/schema" ' +
             'xmlns:s0="http://tempuri.org/" ' +
             'xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution"' );
     
          //Remove any data from the SendXMLElement secondary data source.
          var objData = objSendXMLElement.selectSingleNode(
             "/dfs:myFields/dfs:queryFields/s0:SendXMLElement/s0:theElement");
          var objCurrentData = objData.selectNodes("@* | node()");
          objCurrentData.removeAll();
     
          //Clone the XDocument.
          var objClonedDocument = XDocument.DOM.documentElement.cloneNode(true );
          objData.appendChild( objClonedDocument );
       
          //Call the "Query" method of the secondary data source to send the data.
          XDocument.DataObjects("SendXMLElement").Query();
       
          //Report the results of the submit.
          XDocument.UI.Alert( 
             objSendXMLElement.selectSingleNode(
                "/dfs:myFields/dfs:dataFields/s0:SendXMLElementResponse/s0:SendXMLElementResult").text );
          eventObj.ReturnStatus = true;
       }
       catch(ex)
       {
          eventObj.ReturnStatus = false;
       }

Write Code

  1. On the Tools menu, click Submitting Forms.
  2. In the Submitting Forms dialog box, click Enable Submit.
  3. In the Submit list, click Submit using Custom Script, and then click OK to open Microsoft Script Editor.
  4. In the XDocument::OnSubmitRequest method, use script like the following script to submit the whole form.

    Note You can also submit individual elements in the form.
    try
       { 
          //Create a SOAP object.
          var objSOAPConnector = new ActiveXObject("MSOSOAP.HttpConnector30");
       
          //Set the EndPointURL property to point to the Web service.
          objSOAPConnector.Property("EndPointURL") = "http://server/WebService1/Submit.asmx";
     
          //Set the SoapAction property to point to the Web service method. You can find this URI 
          //in the WSDL file of the Web service.
          objSOAPConnector.Property("SoapAction") = "http://tempuri.org/SendXMLElement";
          objSOAPConnector.Connect();
          
          //Begin construction of a SOAP message to send to the Web service.
          objSOAPConnector.BeginMessage();
     
          var objSOAPSerializer = new ActiveXObject("MSOSoap.SoapSerializer30");
          objSOAPSerializer.Init( objSOAPConnector.InputStream );
          objSOAPSerializer.startEnvelope();
          objSOAPSerializer.startBody();
     
          //Construct the structure that marks the method name and the parameter name 
          //that you are sending.
          objSOAPSerializer.StartElement( "SendXMLElement", "http://tempuri.org/" );
          objSOAPSerializer.StartElement( "theNode", "http://tempuri.org/" );
     
          //Write out the XML of the document.
          objSOAPSerializer.WriteXml( XDocument.DOM.documentElement.xml );
     
          //Finish each element.
          objSOAPSerializer.EndElement();
          objSOAPSerializer.EndElement();
     
          //Call EndMessage to complete the SOAP message and send it to the Web service method.
          //This results in the Web service method being called.
          objSOAPSerializer.endBody();
          objSOAPSerializer.endEnvelope(); 
          objSOAPConnector.EndMessage();
     
          //Use a SoapReader to read the response from the Web service method .
          var ResponseReader = new ActiveXObject("MSOSOAP.SoapReader30");
          ResponseReader.Load( objSOAPConnector.OutputStream );
     
          //If there was no error, return true.
          if (ResponseReader.Fault != null)
          {
             eventObj.ReturnStatus = false;
             throw "Error submitting data: " + ResponseReader.Fault.xml;
          }
          eventObj.ReturnStatus = true;
       } 
       catch (ex)
       { 
          XDocument.UI.Alert("Failed to submit document: " + ex.description); 
       }

Status

This behavior is by design.

This behavior is by design in Microsoft Office InfoPath 2003. This behavior is changed by Microsoft Office InfoPath 2003 Service Pack 1.

More information

Steps to Reproduce the Behavior

  1. Create a Web service that contains the following two methods:
    • Method 1
      [WebMethod]
         public void SendSampleString( string sampleString )
         {
            //Do something interesting with the sample string.
         } 
    • Method 2
      [WebMethod]
         public string SendXMLElement( System.Xml.XmlElement theElement )
         {
            //Report how many children the submitted node had.
            return "Node with " + theElement.ChildNodes.Count + " children submitted.";
         }
  2. Design a new InfoPath form by selecting New from Data Source.
  3. In the Data Source Setup Wizard, select Web Service, and then click Next.
  4. Click Receive and Submit Data, and then click Next.
  5. For the receive data Web service method, follow these steps:
    1. Enter the URL to the Web service that you created earlier, and then click Next.
    2. In the Select an operation list, click SendXMLElement, and then click Next.
  6. For the submit data Web service method, follow these steps:
    1. Enter the URL to the Web service that you created earlier, and then click Next.
    2. In the Select an operation list, click SendXMLElement, and then click Next.
  7. Click the Modify button to choose a parameter for the submit operation.
  8. In the dataFields list, click SendXMLElementResult, click OK, and then click Next.
  9. Select Design data view first, and then click Finish to close the Data Source Setup Wizard dialog box.
  10. Add the SendXMLElementResult field to the form.
  11. Preview the form.
  12. In the Send XML Node Result control, type Hello, World!
  13. On the File menu, click Submit.

    You receive the following error message:
    InfoPath cannot submit the form.
    An error occurred while the form was being submitted.

    Show Details:
    The SOAP response indicates that an error occurred:
    Server was unable to read request. --> There is an error in XML document (1,409). -->Specified cast is not valid.
  14. Close the Preview window, and then return the form to Design mode.
To resolve this problem, use either of the methods that are mentioned in the "Resolution" section of this article.

Properties

Article ID: 826989 - Last Review: December 25, 2014 - Revision: 2.0
Applies to
  • Microsoft Office InfoPath 2003
Keywords: 
kbnosurvey kbarchive kbtshoot kbprb kbbug KB826989

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com