Thursday, December 29, 2011

Adding XSL to a SharePoint Visual Studio Solution

When working with custom SharePoint search results web parts, inheriting from CoreResultsWebpart you’ll often use XSL to transform the search results display (this is not limited to this type of web part, you can use for list views too).  You could deploy the web part and configure the XSL after deployment or you could include the link to your XSL in your web part and package the XSL with your Visual Studio 2010 project.  To do the latter, from your Visual Studio Project, right click the name of the project in the Solution Explorer pane, select Add, and select SharePoint Mapped Folder.


In resulting window, select the folder at path Template\LAYOUTS\XSL to map.  Once mapped, I suggest adding a custom folder so it’s clear that this is custom XSL and then either upload your existing *.xsl file or create a new one and add your XSL (Side Note, SharePoint Designer does a great job of generating XSL based on any sample XML).


In the inherited class’ ConfigureDataSourceProperties set the XslLink property to the path of the XSL in the hive (“/_layouts/xsl/xyzcustom” in this case).

protected override void ConfigureDataSourceProperties()
this.XslLink = "/_layouts/xsl/xyzcustom/mmsearch.xsl";


Now, wherever you deploy the web part and add to a page, it will use your custom XSL.

Using this method, the XSL can not be overridden, If you want to overide after adding the web part, instead add to your web part's properties like this.

<?xml version="1.0" encoding="utf-8"?>
<webPart xmlns="">
<type name="XYZSearchBuilder, $SharePoint.Project.AssemblyFullName$" />
<property name="Title" type="string">My Core Search Results</property>
<property name="Description" type="string">Inherits from Core Results Web Part. This web part provides a fixed query builder configuration to set keyword values to provide results containg the currently logged in user. The web part also provides configuration to sort results by a managed property (reduce storage may need to be set).</property>
<property name="XslLink" type="string">/_layouts/xsl/xyzcustom/xyzsearch.xsl</property>

No comments:

Post a Comment