How to bind a Rich Text Box control to an element that is returned from a Web service in InfoPath 2003 and Visual Studio .NET (2003) (826996)



The information in this article applies to:

  • Microsoft Office InfoPath 2003
  • Microsoft Visual Studio .NET (2003), Enterprise Developer Edition
  • Microsoft Office InfoPath 2003, Service Pack 1 (SP1)

SUMMARY

This article describes how to bind a Rich Text Box control on a Microsoft Office InfoPath 2003 form to an XML element that is returned from a Web service.

For a Rich Text Box control to bind to an XML element that is returned from a Web service, the Rich Text Box control must contain XHTML content. The element must have the following XML schema:
	<xsd:element name="[elementname]">
	  <xsd:complexType mixed="true">
	    <xsd:sequence>
	      <xsd:any namespace="http://www.w3.org/1999/xhtml" processContents="lax"
		minOccurs="0" maxOccurs="unbounded"/>
	    </xsd:sequence>
	  </xsd:complexType>
	</xsd:element>
The <elementname> is the name of the XML element that is returned from the Web service.

InfoPath can automatically detect if an element is an XHTML element by querying the element for a sample value when InfoPath connects to the Web service data source for the first time. This article describes how to create a Web service that returns valid XHTML. This article also describes how to display the XHTML that is returned from the Web service in a Rich Text Box control on an InfoPath form.

back to the top

Create the Web service

  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, click New, and then click Project.
  3. In the Project Types list, click Visual C# Projects. In the Templates list, click ASP.NET Web Service.
  4. In the Location box, type http://<SERVER>/RichTextService where <SERVER> is the name of your Web server, and then click OK.
  5. Right-click Service1.asmx, and then click View Code.
  6. Add the following Web Service method to the Service1 class:
    	[WebMethod]
    	public System.Xml.XmlNode GetXHTMLRichText()
    	{
             //Create a temporary XmlDocument object to generate nodes.
             System.Xml.XmlDocument tempDocument = new System.Xml.XmlDocument();
    	   
             //Create a wrapper node for the data.  This is necessary so InfoPath 
             //correctly detects the XHTML content
             System.Xml.XmlElement theNode = (System.Xml.XmlElement)tempDocument.CreateNode(
                System.Xml.XmlNodeType.Element, "theNode", "http://somearbitrarynamespace/" );
             
             //Create a "font" element in the xhtml namespace.
             System.Xml.XmlElement theFontNode = (System.Xml.XmlElement)tempDocument.CreateNode( 
                System.Xml.XmlNodeType.Element, "font", "http://www.w3.org/1999/xhtml" );
             theFontNode.InnerText= "Red Text";
    	  
             //Add a color attribute.
             System.Xml.XmlAttribute colorAttribute = tempDocument.CreateAttribute( 
                "color" );
             colorAttribute.Value = "#ff0000";
             theFontNode.Attributes.Append( colorAttribute );
    	  
             //Append the font node to the wrapper node
             theNode.AppendChild( theFontNode );
    
             //Return the wrapper element.
             return theNode;
    	}
  7. On the Build menu, click Build Solution.
  8. Exit Visual Studio .NET.
back to the top

Create the InfoPath form

  1. Start InfoPath.
  2. On the File menu, click Design a Form.
  3. In the Design a Form task pane, click New from Data Connection....

    The Data Source Setup Wizard starts.
  4. Setup the data source as follows:
    1. Click Web Service for the data source, and then click Next.
    2. Click Receive data, and then click Next.
    3. Type http://<SERVER>/RichTextService/Service1.asmx for the location of the Web service, and then click Next.
    4. In the Select an operation list, click GetXHTMLRichText, and then click Next.
    5. Click Finish.
  5. Switch to the Data Source task pane, and then expand the dataFields group.
  6. Expand the GetXHTMLRichTextResponse group, and then move the GetXHTMLRichTextResult element to your form.

    InfoPath adds a Rich Text Box control to the view.
back to the top

Try it out

  1. On the Task pane drop-down list, click Views.
  2. In the Views list, click Query. Right-click Query, and then click Set as Default.
  3. On the File menu, point to Preview Form, and then click Default.
  4. Click Run Query.
  5. On the View menu, click Data Entry.

    Notice the value that is in the Rich Text Box control on the form. The value that is returned by the Web service is Red Text, and the value is formatted in red.
back to the top

Modification Type:MinorLast Reviewed:10/21/2004
Keywords:kbHOWTOmaster kbWebServices kbhowto KB826996 kbAudDeveloper