Archive for April, 2011

SharePoint 2010: List column that renders as a dynamic hyperlink

April 18, 2011 27 comments

A while back I worked on a project where we needed to add a column to a SharePoint List that would display a link based on values in other columns. For example: “/myWeb/pages/mycustomPage.aspx?Reference=[ValueToComeFromAnotherColumn] i.e. similar to the way the Title field link is rendered where the URL is dynamic and based on the ID of the row in question.

In MOSS 2007 we did this by adding a calculated column that constructed the HTML using our specified formula. However, the problem was that the link would appear as:

 <a href="myWeb/pages/mycustomPage.aspx?Reference=45">View</a>

To ensure it rendered as a nice and friendly hyperlink we used a script created by Christophe from (his blogs on the subject dont seem to exist anymore). It worked pretty well in MOSS 2007 but we found that in MOSS 2010 it didnt work if your list view had grouping by any column.

Initially I tried to get the script to work but then realised that there was a much better way to do this in SharePoint 2010. In 2010 you can control how any field is rendered by creating a custom XSLT stylesheet. In our scenario we created an XSL file which was deployed at “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\XSL”. The XSL file must be named in the following format fldtypes_*.xsl, where * is any valid filename string value for example fldtypes_MyCustomDefinitions.xsl. Our XSL file looked something like this:

<xsl:stylesheet xmlns:x="" xmlns:d="" version="1.0" exclude-result-prefixes="xsl msxsl ddwrt" xmlns:ddwrt="" xmlns:asp="" xmlns:__designer="" xmlns:xsl="" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:SharePoint="Microsoft.SharePoint.WebControls" xmlns:ddwrt2="urn:frontpage:internal">
  <xsl:template match="FieldRef[@Name='FormLink']" mode="Computed_body">
    <xsl:param name="thisNode" select="."/>
	    <a target="_blank" href="/appeal/pages/appealapplication.aspx?ApplicationRef={$thisNode/@ApplicationRef}">View Application</a>

The important bit is the match attribute in the xsl:template element where we specify the internal name of the Field we want this custom rendering applied to. What this basically does is that where ever a field with an internal name of FormLink is being rendered in a view it will render it as a hyperlink. Below is the definition for our SharePoint List Field:

<Field ReadOnly="TRUE" ID="{2DD3638A-56A0-4021-B2B9-7BBB19E191F1}" Name="FormLink" StaticName="FormLink" DisplayName="Application" Type="Computed" Group="My Custom Group" SourceID="" />

That is all there is to it, now when ever and where ever our field is rendered in a view it renders it in our custom way.

Update: For this to work, the column you are referencing must exist in the view. In my example above I am referencing a column called “ApplicationRef” therefore for it to work in my scenario the SharePoint List View I want it to work in must have “ApplicationRef” column otherwise the value wont be picked up. Hope this makes sense.

I have created a sample Visual Studio SharePoint Project that demonstrates this functionality. Please click on the link below to download it. Just deploy the solution from Visual Studio and it will create a list with a dynamic hyperlink column.


[Update: 08/10/2012] Depending on your scenario you might find this simpler solution more feasible: