# How to attach programmatically and XSL to a View

I have my custom Document Library with his own content type, views and so on...

I'd like to add a particular XSL to a View, to trasform particular value into other.. This works if I put the XSL file in a specific folder... But this way it is applied for all views and this is my problem... So, this is the working XSL:

<?xml version="1.0" encoding="utf-8"?>
<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">

<xsl:template  match="FieldRef[@Name='SvDCollegamento']" mode="body" xmlns:ddwrt2="urn:frontpage:internal">
<xsl:param name='thisNode' select="."/>

<xsl:variable name="Valore" select="$thisNode/@*[name()=current()/@Name]" /> <xsl:if test="$Valore > 0">
<div Title="Seleziona tutti i documenti collegati a questo" style="text-align: center;">
<xsl:value-of select='$thisNode/@*[name()=current()/@Name]' disable-output-escaping ="yes"/> <img src="/_layouts/Images/attach.gif" onClick="javascript: location.href = location.href + '&amp;FilterField10=SvDCollegamento&amp;FilterValue10={$Valore}';"
alt="Seleziona solo i documenti collegati a questo." />
</div>
</xsl:if>

</xsl:template>
</xsl:stylesheet>

I tried to assign it to the SPView.XSL property of my View but now when I try to open that view it doesn't work...

Any suggestion? When I assign the string to the .XSL value, I must follow different rules?

Thank you!!

• Have you tried using the XslLink element to the View definition in Schema.xml? Check out: msdn.microsoft.com/en-us/library/ms459356.aspx. Also, the Blog Site Template uses custom XSL all over. Try looking there for examples on how the SharePoint folks have done this. – Brian Jul 13 '12 at 12:23
• Yes, but I prefer to hard code this thing.. – Ziba Leah Jul 13 '12 at 12:29

Are you just doing this on a field by field basis? If so then you can override the rendering template for a particular field using fldtypes.xsl files. Drop these into templates/layouts/xsl and they magically work.

So if you had a custom field called MyBigField, you can define an XSL just for displaying that field value in a list.

http://msdn.microsoft.com/en-us/library/ff606773.aspx

This is an example of one I did recently where I needed to generate a custom edit URL which incorporated the ID of the current listitem,

<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" ddwrt:oob="true">
<xsl:output method="html" indent="no"/>
<xsl:template name="FieldRef_HRDTEdit_body" ddwrt:dvt_mode="body" match ="FieldRef[@Name='PW_CustomFieldEdit']" mode="Text_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> <xsl:variable name="CustomFieldurl" select="$thisNode/@PW_CustomFieldUrl" />

<xsl:variable name="ID">
<xsl:call-template name="ResolveId">
<xsl:with-param name="thisNode" select ="$thisNode"/> </xsl:call-template> </xsl:variable> <a href="{$CustomFieldurl}/pages/editCustomField.aspx">Edit</a>
</xsl:template>
</xsl:stylesheet>

This can be placed into a file called something like fldtypes_pwcustom.xsl and deployed to C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\XSL

• Hi, I must do it not only for a custom field, but of every column with a specified name.. In that case, how can I attach that programmatically? Which is the tag that must start the xsl? Thank you! – Ziba Leah Jul 16 '12 at 7:20
• It isn't a case of one of the fields 'starting' the XSL. Each template in the XSL matches a particular column. Just add more if you need to do more columns. You need to make sure you have a unique column name though which makes me think in your particular case you might want to go back to assigning a custom XSL to the webpart instance. – PlanetWilson Jul 16 '12 at 9:11
• OK but in the case I want to "use" your code.. inside of the .Xsl property I must set all the code you posted or only the part inside the <xsl:template> tag? – Ziba Leah Jul 16 '12 at 10:05
• The deployed file needs the stylesheet and output nodes, you are simply altering the template nodes, adding or removing them as necessary. Each of those nodes formats a column. In the above exmaple there is one template note. You can have multiple. – PlanetWilson Jul 16 '12 at 10:32