Adding links in SharePoint Data View Web Parts

I am sure this is well documented elsewhere, but it is something that always flummoxes me when I am working on a Data View in SharePoint Designer 2007. Typically you will want to add a link to the field in one of the columns in the data view so users can view the detail. One way to achieve this is with Web Part Connections that allow you to pass filter values to web parts on this or other pages.  However I was looking for a simple hyperlink that would open the list item view using the ID of the record.

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

However simply trying to use the concat function with strings of html fails to parse

<xsl:value-of select="concat(‘<a href="/research/resportal/events/Lists/Events/DispForm.aspx?ID=’, @ows_ID, ‘">’, @ows_LinkTitle, ‘</a>’)" />

There are two solutions to this

  • using the attribute tag: probably the right way to do this
  • creating a variable: almost certainly a lot slower, but more flexible

The Attribute Tag

The facility to add attributes to an <a> is built into XSLT

This approach elegantly mixes HTML and values

 

Creating a variable

The trick is to declare the HTML strings as variables and then concat these variables

<td class="ms-vb"> <xsl:variable name="L1Text">&lt;a href="/Events/DispForm.aspx?ID=</xsl:variable> <xsl:variable name="L2Text">"&gt;</xsl:variable> <xsl:variable name="L3Text">&lt;/a&gt;</xsl:variable> <xsl:value-of select="concat($L1Text, @ows_ID, $L2Text, @ows_LinkTitle, $L3Text)" disable-output-escaping="yes" /> </td>

The only two tricks are

  1. all HTML tags in the strings need to be escaped, i.e. &amp;
  2. set disable-output-escaping="yes"

As the variables are static they could be declared earlier in the XSL so the are not reset on each iteration.