Customising the Content Query Web Part’s RSS feed

If you have ever used the Content Query Web Part (CQWP) you will have noticed that it has an option to return an RSS feed for the items it’s returning. This makes it great for things like “Latest News” sections of a website and things like that. But did you know that you can actually control how that RSS is generated? It’s surprisingly simple – it does an XSL transformation on the XML data returned by the query to turn it into valid RSS XML. Here’s how you go about changing it.

Firstly, find a CQWP that has an RSS feed on it and click on the link, have a look at the URL it is taking you to, it should look something like this:

/_layouts/feed.aspx?xsl=1&web=%2F&page=[GUID]&wp=[GUID]

If you have a look at the default ContentQueryMain.xsl you can see the code that generate the link for you, it looks like this:

<xsl:variable name="FeedUrl1" select="concat($SiteUrl,$FeedPageUrl,'xsl=1&web=',$WebUrl,'&page=',$PageId,'&wp=',$WebPartId)" />

The bit in this that we are concerned with is that “xsl=1″ part. Basically this is what determines the style sheet that gets used. If you get reflector out and have a look at the code behind for that feed.aspx you will see that it goes and does a looks in the web.config file to determine which XSL file it should use to generate the RSS feed. It does this by combining the string “FeedXsl” and the value of that XSL query string parameter. So by default it’s going to go to web.config and get the value of “FeedXsl1″ from the appSettings section, which by default will return “/Style Library/Xsl Style Sheets/Rss.xsl” (I’ve seen that setting there a bunch of times before, but I never really wondered how it worked!).

So now if you go into a publishing site and open up that Rss.xsl file, you will see that it is generating the RSS feed XSL by slipping the appropriate variables into the appropriate places – reasonably straight forward XSL stuff here, nothing special.

So now, if you want to customise the XSL that gets used for a specific CQWP what do you do? It goes a little bit like this:

  • Create a copy of the ContentQueryMain.xsl file and changing the xsl=1 to something else (xsl=2 is a good start :-P)
  • Customising the XSL stylehseet that the CQWP uses (do this by exporting a CQWP and doing a find and replace for contentquerymain.xsl to point it to a new file that is your own copy of the file, than upload it back to your web part gallery and put it back on the page)
  • Adding a web.config entry to the appSettings section called “FeedXsl2″ (or whatever it should be based on the xsl query string attribute) that points to a copy of the Rss.xsl file in your site
  • Then customise that copied Rss.xsl file to control how your RSS is rendered

That’s it – so now if you look at the newly updated CQWP you should see the URL you’re feed points to has xsl=2 in it and the RSS is generated from your custom XSL file.


  1. No trackbacks yet.