I recently ran into an interesting one while building some InfoPath forms for SP2010/2013
forms services.

I wanted to return some Rich Text (XHTML) fields back from a WCF
WebService call.

I was at the point as a developer, where I couldn’t even say ’Works on my
machine
’.

The problem was – no matter what I tried, I would always have *plain text* and no
’richness’ of the Rich Text. Didn’t work for me.

So I have:

1) a basic WCF Web service – running on my dev environment.

2) an InfoPath Form that makes the call and displays the results.

The WCF Service:

image

This is the field that I eventually want to return as RichText to InfoPath.

Here’s the Service Method code (which basically goes into a file and returns back
a list of clauses) – just focus on the CONTENT = GetXHTMLRichText()

image

 

InfoPath and Returning a RichTextField
2 things need to happen for this to work.

1. When InfoPath adds the WCF Service to the form, it needs to ’detect’ the field
correctly when it build the underlying schema.

image

You need (nb – ’Content’ is my field name):
<xs:element minOccurs=”0″ name=”Content” nillable=”true”>
               
<xs:complexType mixed=’true’>
                   
<xs:sequence>
                       
<xs:any minOccurs=”0″ processContents=”lax” maxOccurs=”unbounded” namespace=”http://www.w3.org/1999/xhtml”></xs:any>
                   
</xs:sequence>
               
</xs:complexType>

</xs:element>

Note the namespace on the ANY element above – this is the winner to tell InfoPath
that this is a richtext field.

2. When returning data via this field (in my case the ’Content’ field),
it needs to be in a certain shape, as in:
<Content xmlns=http://yournamespace>
    <span xmlns="http://www.w3.org/1999/xhtml">Rich text here</div>
</Content>


Your rich text content needs to be ’wrapped’ for InfoPath to play nicely with it.

This was the purpose of my GetXMLRichText method as

image

 

The gotcha:

When I pointed InfoPath at my webservice and added a service reference I was getting
back a SimpleType for the field and not a ComplexType/Rich
Text field.

The WCF Service WSDL was ’almost there’ but not close enough:
image

The Content field described in a ComplexType which
is almost there, but not quite.

It’s missing the <xs:complexType mixed=’true’><xs:any namespace=’http://www.w3.org/1999/xhtml’ />.
The rest were good.

The fix:

Cutting a long story short, the simplest way forward here was to simply edit the form
components that InfoPath had built and correct the schema. Then reuse the form.

The form looks like this:

image

 

From the File->Publish->Export Source Files you can get to
the source and edit the correct schema (XSD) file.

image

Close the form down in InfoPath (or you may even need to close InfoPath) to edit the
Schema.

image

You may need to hunt through a few of them to find the right one. My file was GetKCCTerms12.xsd

Modify, save and close that file.

Right click on manifest.xsf -> Design to launch InfoPath and then
select Save As to work with it as *.XSN form (*.xsn files are just
CABs with all these files inside)

image

The final result as viewed from an InfoPath form – notice the bolding sent through.

Enjoy,

Mick.


Blog Post by: Mick Badran