Freigeben über


Customizing Content Query RSS feeds

One of the great features of the Content Query web part is that, once you configure it and have it showing the content that you want, you can have it emit an RSS feed of the same content.  So, if you have a Content Query web part showing the latest news articles published on the site, you can offer readers an RSS feed of this same content for them to subscribe to.

One question that comes out of this is, "How can I change the output of the RSS feed?"  For example, let's say you want to emit a different column of your data as the feed item title or description.  Or, let's say you wanted to offer the feed in a different format, such as Atom.

Let's first take a closer look at the feed that is emitted by the Content Query web part.  Here, I have a CQWP showing the latest news articles on my site, and it has a feed enabled.  By the way, to enable a feed on a Content Query web part, simply edit the web part, expand the Presentation section, and check the Enable Feed box.

If I click through to the feed, I see a preview of it in IE7, and I can subscribe to it from here.  The URL of the feed in this case is https://<servername>/_layouts/feed.aspx?xsl=1&web=%2FNews%2FEnglish&page=fe5ab523-46d2-4285-a095-d590ca8975a5&wp=4144557a-1f5e-4ebf-9ec7-9b1525df1e43

The query string parameters after the feed.aspx page are the key here.  They are:

Parameter name

Description

Valid values

Xsl

The xsl to use to produce the feed

Registered xsls in the web.config file

Web

The web where the CQWP lives that is emitting this feed

Server-relative url

Page

The page where the CQWP lives that is emitting this feed

GUID

Wp

The CQWP ID

GUID

Feed.aspx effectively "looks up" the Content Query web part that is emitting the feed, figures out the query it runs, runs the same query, but instead of outputting HTML, it outputs feed xml by using the XSL in question.

Now, you may be wondering what "XSL=1" is referring to.  That's not a path to an XSL file.  Instead of feed.aspx supporting the ability to run XSLs here, we wanted to make it something that the admin could manage.  So, only XSLs that are registered in the web.config file can be used by feed.aspx.  Looking at the web.config for this site, you'll find the following line:

 
<appsettings>
 ...
    <add key="FeedXsl1" value="/Style Library/Xsl Style Sheets/Rss.xsl" >
 ...
</appsettings>

This is the out of box registered XSL.  You'll notice it lives in the Style Library, right next to the XSLs used to render content in the Content Query web part.

If I wanted to have my own XSL be used for CQWP RSS feeds, I could do a couple of things.  First, I could edit this XSL file.  Then, all CQWPs would use this updated XSL file to render the feeds.  However, if I want something custom that pertains to just one Content Query web part, it makes more sense to add my own XSL file.

To do that, you need to do the following:

  • 1. First, add the XSL file that you want your Content Query web part to use for the feed in the Style Library. You can place it in the same folder as the out-of-box rss.xslt.

  • 2. Next, add a line to the web.config file to register this XSLT file. The important thing is what number(s) or letter(s) you use after the "FeedXsl" token. I'm going to stick to numbers, so I'll use a "2":
 
<appSettings>
 ...
   <add key="FeedXsl1" value="/Style Library/Xsl Style Sheets/Rss.xsl" />
   <add key="FeedXsl2" value="/Style Library/Xsl Style Sheets/Rss-custom.xsl" />
 ...
</appSettings>
  • 3. Now we'll need to modify the XSLT that emits the RSS feed link in the Content Query web part. Again, if I edit the shared ContentQueryMain.xsl, it'll affect all Content Query web parts. Since I only care to modify only one, I'm going to make a copy of this file:

  • 4. And then, I'm going to change one line in it, from:
 
<xsl:variable name="FeedUrl1" select="concat($SiteUrl,$FeedPageUrl,'xsl=1&amp;web=',$WebUrl,'&amp;page=',$PageId,'&amp;wp=',$WebPartId)" />

To:

 
<xsl:variable name="FeedUrl1" select="concat($SiteUrl,$FeedPageUrl,'xsl=2&amp;web=',$WebUrl,'&amp;page=',$PageId,'&amp;wp=',$WebPartId)" />
  • 5. Now, I'll need to ensure my CQWP is using the ContentQueryMain-custom.xsl. To do that, I'll need to edit the web part property that determines which Main file is used. One easy way to do that is to find my web part on a page, export it, edit the property, and re-import it.

The property in question here is:

 
<property name="MainXslLink" type="string" />

For my web part, I'd say:

 
<property name="MainXslLink" type="string">/Style Library/XSL Style Sheets/ContentQueryMain-custom.xsl</property>

That's it!  I now have the ability to have a Content Query web part that's emitting an feed in a custom way.  Hopefully people find this helpful.  And, if people do customize the Content Query web part feeds in this manner, please post a comment and tell us what you did.

--George Perantatos, Program Manager