Microsoft KB Archive/826989

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

Article ID: 826989

Article Last Modified on 7/28/2006

-

APPLIES TO


 * Microsoft Office InfoPath 2003
 * Microsoft Office InfoPath 2003, Service Pack 1 (SP1)

-



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 &quot;Hello, World!&quot; (as you expect). &quot;Hello, World!&quot; 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 &quot;Node with &quot; + theElement.ChildNodes.Count + &quot; children submitted.&quot;; } 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 (&quot;Hello, World!&quot;), 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
 On the Tools menu, click Secondary Data Sources. In the Secondary Data Sources dialog box, click Add. On the first page of the Data Source Setup Wizard, click Web Service, and then click Next. Enter the location of the Web service that you want to submit, and then click Next. Select the Web service method that you want to use, and then click Next.</li> 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.</li> Click to clear the &quot;Connect to this secondary data source when the form is opened&quot; check box, click Finish, and then click OK to close the Secondary Data Sources dialog box.</li> On the Tools menu, click Submitting Forms.</li> In the Submitting Forms dialog box, click Enable Submit.</li> In the Submit list, click Submit using Custom Script, and then click OK to open Microsoft Script Editor.</li>  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(&quot;SendXMLElement&quot;); objSendXMLElement.setProperty( &quot;SelectionNamespaces&quot;,        'xmlns:s1=&quot;http://joelallxp.microsoft.com/schema&quot; ' +         'xmlns:s0=&quot;http://tempuri.org/&quot; ' +         'xmlns:dfs=&quot;http://schemas.microsoft.com/office/infopath/2003/dataFormSolution&quot;' ); //Remove any data from the SendXMLElement secondary data source. var objData = objSendXMLElement.selectSingleNode(        &quot;/dfs:myFields/dfs:queryFields/s0:SendXMLElement/s0:theElement&quot;); var objCurrentData = objData.selectNodes(&quot;@* | node&quot;); objCurrentData.removeAll; //Clone the XDocument. var objClonedDocument = XDocument.DOM.documentElement.cloneNode(true ); objData.appendChild( objClonedDocument ); //Call the &quot;Query&quot; method of the secondary data source to send the data. XDocument.DataObjects(&quot;SendXMLElement&quot;).Query; //Report the results of the submit. XDocument.UI.Alert(         objSendXMLElement.selectSingleNode( &quot;/dfs:myFields/dfs:dataFields/s0:SendXMLElementResponse/s0:SendXMLElementResult&quot;).text ); eventObj.ReturnStatus = true; }  catch(ex) {     eventObj.ReturnStatus = false; } </li></ol>

Write Code
<ol> On the Tools menu, click Submitting Forms.</li> In the Submitting Forms dialog box, click Enable Submit.</li> In the Submit list, click Submit using Custom Script, and then click OK to open Microsoft Script Editor.</li>  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(&quot;MSOSOAP.HttpConnector30&quot;); //Set the EndPointURL property to point to the Web service. objSOAPConnector.Property(&quot;EndPointURL&quot;) = &quot;http://server/WebService1/Submit.asmx&quot;; //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(&quot;SoapAction&quot;) = &quot;http://tempuri.org/SendXMLElement&quot;; objSOAPConnector.Connect; //Begin construction of a SOAP message to send to the Web service. objSOAPConnector.BeginMessage; var objSOAPSerializer = new ActiveXObject(&quot;MSOSoap.SoapSerializer30&quot;); 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( &quot;SendXMLElement&quot;, &quot;http://tempuri.org/&quot; ); objSOAPSerializer.StartElement( &quot;theNode&quot;, &quot;http://tempuri.org/&quot; ); //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(&quot;MSOSOAP.SoapReader30&quot;); ResponseReader.Load( objSOAPConnector.OutputStream ); //If there was no error, return true. if (ResponseReader.Fault != null) {        eventObj.ReturnStatus = false; throw &quot;Error submitting data: &quot; + ResponseReader.Fault.xml; }     eventObj.ReturnStatus = true; }   catch (ex) {      XDocument.UI.Alert(&quot;Failed to submit document: &quot; + ex.description); } </li></ol>

<div class="status_section">

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.

<div class="moreinformation_section">

Steps to Reproduce the Behavior
<ol> Create a Web service that contains the following two methods: <ul>  Method 1 [WebMethod] public void SendSampleString( string sampleString ) {     //Do something interesting with the sample string. } </li>  Method 2 [WebMethod] public string SendXMLElement( System.Xml.XmlElement theElement ) {     //Report how many children the submitted node had. return &quot;Node with &quot; + theElement.ChildNodes.Count + &quot; children submitted.&quot;; } </li></ul> </li> Design a new InfoPath form by selecting New from Data Source.</li> In the Data Source Setup Wizard, select Web Service, and then click Next.</li> Click Receive and Submit Data, and then click Next.</li> <li>For the receive data Web service method, follow these steps: <ol style="list-style-type: lower-alpha;"> <li>Enter the URL to the Web service that you created earlier, and then click Next.</li> <li>In the Select an operation list, click SendXMLElement, and then click Next.</li></ol> </li> <li>For the submit data Web service method, follow these steps: <ol style="list-style-type: lower-alpha;"> <li>Enter the URL to the Web service that you created earlier, and then click Next.</li> <li>In the Select an operation list, click SendXMLElement, and then click Next.</li></ol> </li> <li>Click the Modify button to choose a parameter for the submit operation.</li> <li>In the dataFields list, click SendXMLElementResult, click OK, and then click Next.</li> <li>Select Design data view first, and then click Finish to close the Data Source Setup Wizard dialog box.</li> <li>Add the SendXMLElementResult field to the form.</li> <li>Preview the form.</li> <li>In the Send XML Node Result control, type Hello, World! </li> <li>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.

</li> <li>Close the Preview window, and then return the form to Design mode.</li></ol>

To resolve this problem, use either of the methods that are mentioned in the &quot;Resolution&quot; section of this article.

Keywords: kbtshoot kbbug kbprb KB826989

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.