Tuesday 28 February 2012

Override XSL templates in XSLT List View Web Part


You don’t even need to edit the page in SPD. XSLT List View Web Part has the ability to specify a XSL file through web part property, and the templates defined in the file will take precedence over the OOTB XSL code.  Now you can override the XSL templates selectively. Just save the custom XSL file in a library and point to it.
 In the example below I redefined the template for Description column:

<xsl:stylesheet xmlns:x="http://www.w3.org/2001/XMLSchema" xmlns:d="http://schemas.microsoft.com/sharepoint/dsp" version="1.0" exclude-result-prefixes="xsl msxsl ddwrt" xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" xmlns:asp="http://schemas.microsoft.com/ASPNET/20" xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:SharePoint="Microsoft.SharePoint.WebControls" xmlns:ddwrt2="urn:frontpage:internal" xmlns:o="urn:schemas-microsoft-com:office:office" ddwrt:ghost="show_all"
  <xsl:include href="/_layouts/xsl/main.xsl"/> 
  <xsl:include href="/_layouts/xsl/internal.xsl"/>   
   <xsl:template name="FieldRef_Text_body.Description" ddwrt:dvt_mode="body" match ="FieldRef[@Name='Description']" mode="Text_body" ddwrt:ghost="hide">
  <xsl:param name="thisNode" select="."/>
  <xsl:value-of select="$thisNode/@Description" />
 </xsl:template>  
</xsl:stylesheet>

In order to view the raw xml returned by the query, point to another custom XSL file that contains the following content:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
  <xsl:template match="/">
    <xmp>
      <xsl:copy-of select="*"/>
    </xmp>
  </xsl:template>
</xsl:stylesheet>

1 comment:

  1. Awesome.. short, clear and crisp explanation.. this is what I was searching for a long time.

    ReplyDelete