Rendering a Field with the Custom XSLT Stylesheet
In MOSS 2007, how a field is rendered in a list view relies on its <RenderPattern> element which defines the actual CAML that SharePoint can use to render the field. However in SharePoint 2010, the list view rendering system has been changed significantly to rely on the XSLT stylesheets. To understand how different are the two ways, let’s take a build-in column, Target Audiences, as an example.
In both MOSS 2007 and SharePoint 2010, when we enable target audiences setting on a list, a Target Audiences column will be added to the list. Its field type definition is like the following.
<?xml version="1.0" encoding="utf-8" ?>
<FieldTypes>
<FieldType>
<Field Name="TypeName">TargetTo</Field>
<Field Name="ParentType">Note</Field>
<Field Name="TypeDisplayName">
$Resources:spscore,FieldTypes_Audience_DisplayName;
</Field>
<Field Name="TypeShortDescription">
$Resources:spscore,FieldTypes_Audience_ShortDescription;
</Field>
<Field Name="UserCreatable">FALSE</Field>
<Field Name="FieldTypeClass">
Microsoft.Office.Server.WebControls.FieldTypes.SPFieldTargetTo,Microsoft.Office.Server,Version=12.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c
</Field>
<Field Name="ShowOnSurveyCreate">FALSE</Field>
<PropertySchema>
<Fields>
<Field Name="AllowGlobalAudience"
DisplayName="$Resources:spscore,FieldTypes_Audience_AllowGlobalAudience;"
Type="Boolean">
<Default>1</Default>
</Field>
<Field Name="AllowDL"
DisplayName="$Resources:spscore,FieldTypes_Audience_AllowDistributionList;"
Type="Boolean">
<Default>1</Default>
</Field>
<Field Name="AllowSPGroup"
DisplayName="$Resources:spscore,FieldTypes_Audience_AllowSharepointGroup;"
Type="Boolean">
<Default>1</Default>
</Field>
</Fields>
</PropertySchema>
<RenderPattern Name="DisplayPattern">
<Switch>
<Expr><Column/></Expr>
<Case Value="">
$Resources:spscore,FieldTypes_Audience_NotTargeted;
</Case>
<Case Value=";;;;">
$Resources:spscore,FieldTypes_Audience_NotTargeted;
</Case>
<Default>
$Resources:spscore,FieldTypes_Audience_Targeted;
</Default>
</Switch>
</RenderPattern>
</FieldType>
</FieldTypes>
The <RenderPattern> here defines how this field is rendered. That is, when the value of this field is empty or “;;;;”, it shows No. Otherwise, it shows Yes. The following is a screenshot of this column in a MOSS list.
However in SharePoint 2010, although the field type definition of this field is the same as the definition in MOSS, the rendering in the list view is different. The following is a screenshot in a SharePoint 2010 list.
Why the rendering is so different is because by default SharePoint 2010 does not use <RenderPattern> anymore when rendering a field in the list view. Instead, SharePoint 2010 uses XSLT stylesheets to transform the source tree of the list view into the result tree, and the location of the OOB xsl files is %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\LAYOUTS\XSL.
To make the Target Audiences column to be rendered the same as it rendered in MOSS, we just need to create a custom XSLT stylesheet, put it in the above folder and reset the application pool. The following is the XSLT I wrote to render this column.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:x="https://www.w3.org/2001/XMLSchema"
xmlns:d="https://schemas.microsoft.com/sharepoint/dsp"
version="1.0"
exclude-result-prefixes="xsl msxsl ddwrt"
xmlns:ddwrt="https://schemas.microsoft.com/WebParts/v2/DataView/runtime"
xmlns:asp="https://schemas.microsoft.com/ASPNET/20"
xmlns:__designer="https://schemas.microsoft.com/WebParts/v2/DataView/designer"
xmlns:xsl="https://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:SharePoint="Microsoft.SharePoint.WebControls"
xmlns:ddwrt2="urn:frontpage:internal">
<xsl:template match="FieldRef[@Name='Target_x0020_Audiences']"
mode="Note_body">
<xsl:param name="thisNode" select="." />
<xsl:variable name="FieldValue">
<xsl:call-template name="FieldRef_ValueOf_DisableEscape">
<xsl:with-param name="thisNode" select="$thisNode" />
</xsl:call-template>
</xsl:variable>
<span style="color:red">
<xsl:choose>
<xsl:when test="$FieldValue='' or $FieldValue=';;;;'">
No
</xsl:when>
<xsl:otherwise>
Yes
</xsl:otherwise>
</xsl:choose>
</span>
</xsl:template>
</xsl:stylesheet>
After applying the above XSLT, the Target Audiences column is rendered as below.
Finally, if you want SharePoint 2010 to render your field with <RenderPattern> anyway, you must include a <Field Name="CAMLRendering">TRUE</Field> in your field type definition.
Reference: Overview of XSLT List View Rendering System
Comments
- Anonymous
February 14, 2011
And what about, to get the value of the field in the relatedfield property in the xsl of the related field isn't show in the view?Thanks - Anonymous
February 14, 2011
And what about, to get the value of the field in the relatedfield property in the xsl if the related field isn't show in the view?Thanks - Anonymous
March 14, 2011
HiI m using Rdio Button in property schema n now i want get its value in Rendering pattern.I use like this :<FieldSwitch> <Expr> <Property Select='Value'/> </Expr>but still its going to default.Please help me out.Thanks. - Anonymous
October 04, 2013
Is it possible to do it the other way round? IE to show the actual audience names (as 2010 does) but in MOSS 2007.