Problem:
I want to deploy my custom style for the Content Query Web Part (CQWP).
The CQWP is using the following files to style its presentation: ContentQueryMain.xsl, Header.xsl and ItemStyle.xsl. The files are located under /Style Library/XSL Style Sheets in the root site.
The first solution that comes into my mind is to replace the OOTB xslt files with our customized files. It will work if you do it as a manual operation from the Sharepoint UI but it will not work from the site definition (onet.xml) or from a feature. Also this solution doesn’t provide you an independent solution that doesn’t interfere with Microsoft possible updates.
Solution:
ContentByQueryWebPart is the CQWP class (a public class). It has a constructor with 3 parameters that provides a hook to specify the relative URLs to the files used for styling. You have to create a light class that inherits from ContentByQueryWebPart and which has the following implementation for the default constructor:
public class MyCustomContentQueryWebPart : ContentByQueryWebPart
{
public MyCustomContentQueryWebPart()
: base(”/Style Library/XSL Style Sheets/MyCustomContentQueryMain.xsl”, “/Style Library/XSL Style Sheets/MyCustomHeader.xsl”, “/Style Library/XSL Style Sheets/MyCustomItemStyle.xsl”)
{
}
}
MyCustomContentQueryMain.xsl, MyCustomHeader.xsl and MyCustomItem.xsl can be created copying the OOTB files and amending them. The xslt files can be deployed to the /Style Library/XSL Style Sheets directly from the onet.xm file if you have your custom site definition or using a feature.

