Wednesday, April 11, 2012

How To: Enable Embedded HTML in a Calculated Column

We had implemented the calculated column to HTML JavaScript that can be found on the Path to SharePoint website (   This was a great solution at the time, and I really appreciate the work they did over at Path to SharePoint to create this method.

This solution had worked well, but in order to make it work across the entire site the JavaScript had been embedded in the Master Page on each site that used a calculated column to display HTML.  We were using the calculated column to create a status indicator on projects, and the column was named the same thing on each site.  Luckily, Microsoft has provided a better way for this scenario.

Microsoft has a great article on using XSLT style sheets to customize the rendering of list views (  By creating an XSLT style sheet for specific column you can change how that column is rendered.  In the use-case above the column could be customized by creating a new fldtypes_*.xsl file.  A fldtypes.xsl file customizes the way that SharePoint renders a specific column type.  It can also be used to customize how SharePoint renders a column of a specific name.

Let's look at an example:
I created a file called fldtypes_ScenicSP.xsl and placed it in C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\XSL on my web front end.  Inside this file I placed the following style sheet:

<xsl:stylesheet xmlns:x=""
                exclude-result-prefixes="xsl msxsl ddwrt"

  <xsl:template match="FieldRef[@Name='Status_x0020_Indicator']" mode="Text_body">
    <xsl:param name="thisNode" select="."/>
    <xsl:value-of select="$thisNode/@*[name()=current()/@Name]" disable-output-escaping ="yes" />

In the style sheet I have a match condition of FieldRef[@Name='Status_x0020_Indicator'].  This indicates that I want this style sheet to apply to all fields that are named "Status Indicator".  You need to be careful not to pick a field name that is going to be commonly used for a lot of different tasks.  This basically makes that field name reserved as a special field that will be treated by SharePoint differently than any other field.  The behavior I changed is to add the following: disable-output-escaping ="yes".  This tells SharePoint that when it renders that field to render the HTML instead of displaying the tags.  The select clause in the value-of is explained in much more detail on the Microsoft article posted above (which I highly encourage you to read so that you understand this principle).

Once I save my file I then need to recycle the application pool on any web application where I intend on using this new style sheet (I suggest doing an IISReset so that all application pools are cycled).  This same file will need to be copied to each web front end and the application pool will need to be cycled on each as well.  Once this has been done you can now see the fruits of your labor.

First, create a task list (I called mine myTasks):

Second, I added a column called Indicator.  I created it as a choice column with three choices (red, yellow, or green). I also created a record

Third, I created a calculated column that generates an image URL based on the Indicator color.

Finally, I added one more calculated column named Status Indicator that creates an image tag.  Status Indicator was the column above for which we changed the rendering behavior.

The finished product can be seen below:

This was a very simple example and can be replicated using an out of the box KPI list, but I will post some more useful examples in a second article on this topic. Please make sure you read the Microsoft article above.  They do a great example of making negative values turn red on a currency column.

No comments:

Post a Comment