This topic has not yet been rated - Rate this topic

Rich Text and Web Services [InfoPath 2003 SDK Documentation]

Office 2003

Applies to:

Microsoft Office InfoPath 2003
Microsoft Office InfoPath 2003 Service Pack 1

Microsoft InfoPath supports binding of a rich text box control in a form to an XML element that is received from a Web service, and submitting data from a rich text box control to an XML element through a Web service. The element must adhere to the Extensible HyperText Markup Language (XHTML) format. For example, the schema for an element named MyRichTextElement that contains rich text would have the following XML schema definition:

    <xsd:element name="MyRichTextElement">
    <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>
  

Before a rich text box control can be bound with the XHTML element, the element should be wrapped with a wrapper node; this wrapper node can belong to any arbitrary namespace. The wrapper node can look like this:

    <xhtmlNode xmlns="http://someNamespace">
    <div xmlns="http://www.w3.org/1999/xhtml">Your rich text here</div>
</xhtmlNode>

This topic outlines the process of creating a Web service that can send and receive XHTML, and how to use InfoPath to bind to the Web service parameters. This topic does not provide detailed instructions on how to create such a Web service. It is assumed that you already have some familiarity with working with Web services.

The example Web service stores the XHTML data that it sends and receives in an XML file on the server. This file, named out.xml, acts as a data source that stores the XHTML data. There are two Web methods that will be exposed to allow a client application to interface with the XHTML data source: getXhtml and setXhtml. The getXhtml Web method returns an XmlNode that contains the XHTML that can be bound to an InfoPath rich text box control. The setXhtml Web method accepts an XmlNode as the data to store in the out.xml file.

Note  These Web methods require using statements that reference the System.IO and System.Xml namespaces.

The getXhtml Web method attempts to load the XML data to be returned from the out.xml file in the Data folder on C: drive. If it fails, because, for example, the file is not found, or does not contain valid XML, the method will simply return an empty DIV HTML element that references the XHTML namespace.

        [WebMethod] 
public XmlNode getXhtml() 
{ 
            // This is the returned XmlDocument upon Query from InfoPath
            XmlDocument document = new XmlDocument();

            // Create a wrapping node with the name of the rich text field.
            // The "http://someNameSpace" can be any arbitrary namespace
            XmlNode richNode = document.CreateNode
                        (XmlNodeType.Element, "MyRichTextElement", "http://someNameSpace");

            // Temporary XmlDocument
            XmlDocument tempDocument = new XmlDocument();
            try
            {
                // Read the saved rich text data from the local machine
                tempDocument.Load(@"c:\Data\out.xml");
            }
            catch (XmlException)
            {
                // If the file does not exist or content is not valid XML
                tempDocument.LoadXml("<div xmlns=\"http://www.w3.org/1999/xhtml\"></div>");
            }

            // Add the file content to the xml
            richNode.AppendChild
                        (document.ImportNode(tempDocument.DocumentElement, true));

            return richNode;
} 

      

The setXhtml Web method accepts XHTML from a rich text box control on an InfoPath form. Since Web services do not support a node list, when a rich text field containing multiple lines is sent to a Web service, the Web service only accepts the first line and ignores the rest.

The sample setXhtml method assumes that it will receive a top-level XML node, which in most cases will be wrapped in a DIV element. If the XML received does not contain a wrapping element, for example when text within the rich text box control has no formatting, this method will detect this by checking whether the NodeType property indicates that the XML passed in is a text node. If the XML is a text node, the method creates a DIV element and copies the text node contents to the DIV, so that the DIV contains a text node child with the text that was sent to the Web service. The XML received by this method is written to the out.xml file in the Data folder on the C: drive.

Note  The sample setXhtml method was written to accept XHTML data of any size. In actual practice, you should always check to see how much data is being submitted and set an upper limit for the amount of data that can be submitted.

        [WebMethod] 
public void setXhtml(XmlNode xn) 
{ 
            XmlDocument document = new XmlDocument();

            if (xn == null)
            {
                // If nothing was submitted or the rich text field is empty, create a DIV
                // that references the XHTML namespace
                XmlElement div = document.CreateElement("div", "http://www.w3.org/1999/xhtml");
                // Copy the node to our own XmlDocument
                document.AppendChild(div);
            }
            if (xn.NodeType == XmlNodeType.Text)
            {
                // If plain text is passed in, wrap it in a DIV
                // that references the XHTML namespace
                XmlElement div = document.CreateElement("div", "http://www.w3.org/1999/xhtml");
                // Copy the text to the DIV.
                div.AppendChild(document.ImportNode(xn, true));
                // Copy the node to our own XmlDocument
                document.AppendChild(div);
            }
            else
            {
                // Copy the node to our own XmlDocument
                document.AppendChild(document.ImportNode(xn, true));
            }

            // Save the file to the local machine
            document.Save(@"c:\Data\out.xml");
} 

      

To create a form that connects to the sample Web service, perform the following steps.

  1. Open InfoPath.
  2. In the task pane, click Design a Form, and then click New from Data Source.
  3. Select Web service as the type of data source, and then click Next.
  4. Select Receive and submit data, and then click Next.
  5. Type the address of the Web service that contains the sample Web service methods, and then click Next.
  6. For the receive method, select getXhtml as the operation, and then click Next.
  7. The getXHTML Web service method takes no parameters, so click Next.
  8. Click Next to use the same Web service to the submit data.
  9. For the submit method, select setXhtml as the operation, and then click Next.
  10. Click Modify, expand the dataFields folder, expand thes0:getXhtmlResponse folder, expand the getXhtmlResult folder, select the MyRichTextElement element, and then click Next.
  11. Click Finish.
  12. In the Data Source task pane, expand the dataFields folder.
  13. Expand the s0:getXhtmlResponse and getXhtmlResult folders, and then drag the MyRichTextElement element onto the form. InfoPath will recognize that the MyRichTextElement element is an XHTML element and will use a rich text box control to bind to it.
  14. Save or publish the form.

To test the form, open the form, enter some rich text content such as pictures, tables, and formatted text. Click Submit on the File menu to store the rich text content in the out.xml file on the server. Click Query on the View menu, and then click the Run Query button on the form. The rich text box should display the XHTML content from the out.xml file. If the rich text field contains multiple lines, the Web service will only accept the first line and ignores the rest.





Did you find this helpful?
(1500 characters remaining)
© 2013 Microsoft. All rights reserved.