Tuesday, January 23, 2007

Showing the description of a page in the Content Query Web Part as rich HTML

The Content query web part is a wonderful way to aggregate data into a page. A common use of it is to show "news" pages in the home page of a publishing site. This is well enough, and it even has several styles to pick from :

If you want to customize one of the styles or create your own you can do that by editing the xslt data

Some of the built-in styles include the description field and display it, but my problem was that the description was plain text. It even ignored line breaks that the users wrote. And that is a big problem if you want a summary of the article in your home page with a "read more..." link. For example, if the description the user wanted on the home page was:

This is a very important description
that has a line break.

He would instead get:
This is a very important description that has a line break.

Solution?Since the pages sit in a document library, and since document libraries do not support rich text fields (damn it!), I had to think of a quick and ugly solution - paste html text into the description. So if the description was to be:

This is a very important description
that has a line break.

the user had to paste the html equivalent:

This is a very <strong><font color="red">important</font></strong> description<br>that has a line break.


So I tried this, and found a flaw. The Content Query web part would display the text as-is and would not format it as rich html.

XSL to the rescue

To resolve this, there is a simple tweak you can do on the itemstyle.xsl file in the site. Open the site in SharePoint Designer, and navigate to the "Style Library" folder, and into the "XSL Style Sheets" folder. Check out and open the ItemStyle.xsl file, and in it find the style that you are using in the webpart, where you want to display the description as rich text.

A quick way to do that would be to search the file for the text

<xsl:value-of select="@Description" />

inside the tag, so that the end result is:

<xsl:value-of select="@Description" disable-output-escaping="yes" />

With this quick change, the html code the user inputs in the description field will be displayed as rich text. The only danger in this is that the user will make a mistake, not close one of the fields and cause havoc on the page. So make sure your users are aware of the risk, and maybe write an event handler or workflow to validate the html. I would recommend just implementing an approval workflow that checks the validity of the html.


AmericanTestament.com said...

Great post. Except in Sharepoint Designer I can't seem to find the folders you're talking about. I did a "find" for "@Description" through all the files and folders I can see in the tree but nothing came up.

Do I have to have more access, such as a server administrator, to make this change? Right now I'm just a site owner/administrator.

Ishai Sagi [SharePoint MVP] said...

Rob - do the search inside the xslt file I mention in the article. not on all files and folders.

Harish Mathanan said...

Brilliant yet simple! Thank you Ishai