Microsoft KB Archive/826996

= 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 =

Article ID: 826996

Article Last Modified on 1/30/2007

-

APPLIES TO


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

-



IN THIS TASK

 * SUMMARY
 * Create the Web service
 * Create the InfoPath form
 * Try it out



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:        The  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
 Start Microsoft Visual Studio .NET. On the File menu, click New, and then click Project. In the Project Types list, click Visual C# Projects. In the Templates list, click ASP.NET Web Service. In the Location box, type http://<SERVER>/RichTextService where  is the name of your Web server, and then click OK.</li> Right-click Service1.asmx, and then click View Code.</li>  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, &quot;theNode&quot;, &quot;http://somearbitrarynamespace/&quot; ); //Create a &quot;font&quot; element in the xhtml namespace. System.Xml.XmlElement theFontNode = (System.Xml.XmlElement)tempDocument.CreateNode(            System.Xml.XmlNodeType.Element, &quot;font&quot;, &quot;http://www.w3.org/1999/xhtml&quot; ); theFontNode.InnerText= &quot;Red Text&quot;; //Add a color attribute. System.Xml.XmlAttribute colorAttribute = tempDocument.CreateAttribute(            &quot;color&quot; ); colorAttribute.Value = &quot;#ff0000&quot;; theFontNode.Attributes.Append( colorAttribute ); //Append the font node to the wrapper node theNode.AppendChild( theFontNode );

//Return the wrapper element. return theNode; } </li> On the Build menu, click Build Solution.</li> Exit Visual Studio .NET.</li></ol>

back to the top

In InfoPath 2003
<ol> Start InfoPath 2003.</li> On the File menu, click Design a Form.</li> In the Design a Form task pane, click New from Data Connection....

The Data Source Setup Wizard starts.</li> Setup the data source as follows: <ol style="list-style-type: lower-alpha;"> Click Web Service for the data source, and then click Next.</li> Click Receive data, and then click Next.</li> Type http://<SERVER>/RichTextService/Service1.asmx for the location of the Web service, and then click Next.</li> In the Select an operation list, click GetXHTMLRichText, and then click Next.</li> Click Finish.</li></ol> </li> Switch to the Data Source task pane, and then expand the dataFields group.</li> Expand the GetXHTMLRichTextResponse group, and then move the GetXHTMLRichTextResult element to your form.

InfoPath adds a Rich Text Box control to the view.</li></ol>

In InfoPath 2007
<ol> <li>Start InfoPath 2007.</li> <li>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.</li> <li>On the Tools menu, click Data Connections.</li> <li>In the Data Connections window, click Add.

The Data Source Setup Wizard starts.</li> <li>Set up the data source as follows: <ol style="list-style-type: lower-alpha;"> <li>Click to select Create a new connection to, click to select Receive data, and then click Next.</li> <li>Click to select Web Service for the data source, and then click Next.</li> <li>Type http://<SERVER>/RichTextService/Service1.asmx for the location of the Web service, and then click Next.</li> <li>In the Select an operation list, click GetXHTMLRichText, and then click Next.</li> <li>Click Finish.</li></ol> </li> <li>Switch to the Data Source task pane, and then expand the dataFields group.</li> <li>Expand the GetXHTMLRichTextResponse group, and then move the GetXHTMLRichTextResult element to the form.

InfoPath adds a Rich Text Box control to the view.</li></ol>

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

Additional query words: IP2007

Keywords: kbhowtomaster kbwebservices kbhowto KB826996

-

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

© Microsoft Corporation. All rights reserved.