On a recent project, we were rolling up discussion board posts, which include a RichHTML field. The design called for the contents of the most recent posts to be summarized on the site’s landing page using a Content Query Web Part (CQWP).
The RichHTML field in the post is a mixed blessing. Having the ability to supply rich formatting within a discussion board post? Great. Having that markup screw up the styling of the landing page when it’s rolled up? Less great.
So we set out to display just the content of each post without the formatting. The solution turned out to be reasonably straightforward.
(Note: If you’re not familiar with customizing the ItemStyle.xsl or ContentQueryMain.xsl files to format CQWP output, start here.)
The first step was to find some XSL that removes the HTML markup, which I found here; this post adds just a bit more context.
I put this XSL template into the ContentQueryMain.xsl file.
And then this additional template goes in the ItemStyle.xsl file:
Note that the pureTextBody XSL variable (line 16) is called by passing the Body attribute to the OuterTemplate.RemoveHtmlTags template (lines 17-18) that we put into the ContentQueryMain.xsl file — and then that variable, cleansed of its HTML markup, is rendered as output (line 32).
That’s it. Now the content of the discussion posts roll up and have a consistent presentation on the site landing page, but have their formatting intact when the user clicks through to look at the full post.