ben tedder : code things

Link an item to its URL with itemstyle.xsl in SharePoint 2010

I'll show you how to link an item to its URL within itemstyle.xsl so your URLs show up friendly in Content Query WebParts. It's just a few lines of code.

First, we need to call the "SafeLinkUrl" variable. That variable calls the GetSafeLink template with the UrlColumnName. All that to say…this is the piece of code you need:

<xsl:template name="ItemListCat" match="Row[@Style='ItemListCat']" mode="itemstyle">
<xsl:variable name="SafeLinkUrl">
  <xsl:call-template name="OuterTemplate.GetSafeLink">
   <xsl:with-param name="UrlColumnName" select="'LinkUrl'"/>
  </xsl:call-template>
</xsl:variable>

Once you've called that variable, you can link it using an href of {$SafeLinkUrl}. See my full code of a sample template that shows a link to the title below.

<xsl:template name="ItemList" match="Row[@Style='ItemList']" mode="itemstyle">
  <xsl:variable name="SafeLinkUrl">
    <xsl:call-template name="OuterTemplate.GetSafeLink">
      <xsl:with-param name="UrlColumnName" select="'LinkUrl'"/>
    </xsl:call-template>
  </xsl:variable>
 <a href="{$SafeLinkUrl}"><xsl:value-of select="@Title"/></a>
</xsl:template>