Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

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


View products that this article applies to.

Summary

This article describes how to bind a Rich Text Box control on a Microsoft Office InfoPath 2007 form or 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.

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.

Create the InfoPath form

In InfoPath 2003

  1. Start InfoPath 2003.
  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.

In InfoPath 2007

  1. Start InfoPath 2007.
  2. In the left pane of the Getting Started dialog box, click Design a Form Template.
    In the Design a Form Template window, click Blank, and then click OK.
  3. On the Tools menu, click Data Connections.
  4. In the Data Connections window, click Add.

    The Data Source Setup Wizard starts.
  5. Set up the data source as follows:
    1. Click to select Create a new connection to, click to select Receive data, and then click Next.
    2. Click to select Web Service for the data source, 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.
  6. Switch to the Data Source task pane, and then expand the dataFields group.
  7. Expand the GetXHTMLRichTextResponse group, and then move the GetXHTMLRichTextResult element to the form.

    InfoPath adds a Rich Text Box control to the view.

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


Keywords: KB826996, kbhowto, kbwebservices, kbhowtomaster

↑ Back to the top

Article Info
Article ID : 826996
Revision : 6
Created on : 1/30/2007
Published on : 1/30/2007
Exists online : False
Views : 505