Creating better RSS feeds with SharePoint 2010 Content Query Web Part


Content Query Web Part (CQWP) allows you to easily create RSS feeds for your content. With SharePoint 2010 it became even easier as you can use slots to map your fields to RSS fields. There are however a few things you should keep in mind to get the most out of an RSS feed created using the CQWP. Find out how to create a proper RSS feed with the SharePoint 2010 Content Query Web Part.

RSS is not dead!

RSS feeds are a great way of delivering information to your audience. Despite of the growth of social media in the last few years, many people still use RSS feeds to stay up-to-date with new content. Using RSS turns out to be a more convenient way of tracking new content rather than using social media. Especially if you’re using a feed reader it is easier for you to be sure that you are not missing anything.

Publishing RSS feeds in SharePoint 2010

SharePoint 2010 provides a number of ways for delivering RSS feeds to visitors of your website. If your content is stored in a single list you might want to use the List RSS feed. Although it’s disabled by default you can enable it by changing a few configuration settings.

Publishing RSS feeds using the List RSS Feed

The first thing that you have to do, to publish an RSS feed using the List RSS Feed is to ensure that RSS feeds have been enabled for the Site Collection. For this go to Site Actions > Site Settings > Site Administration > RSS and check the Allow RSS feeds in this site collection checkbox.

The ‘Allow RSS feeds in this site collection’ setting enabled on the RSS settings page

If you go to the settings of your list now, you should see the RSS settings option in the Communications section.

The ‘RSS settings’ option visible in the Communications section of the List settings page

By adjusting the various settings you can change the way your RSS feed will appear to your readers.

Sample RSS Feed generated using the List RSS Feed

Configuring the List RSS Feed for Publishing Pages

If you want to use the List RSS Feed for Publishing Pages there is one more thing that you have to keep in mind. By default the List RSS Feed will link to the Display Form of the List Item. While this might work on an intranet, it’s definitely not something you would want on an Internet-facing website, considering that anonymous users cannot open the Display Form.

In order to make the List RSS Feed link directly to the Publishing Page instead to its Display Form, you have to go to the List RSS settings and in the Document Options section select the Link RSS items directly to their files option.

The ‘Link RSS items directly to their files’ setting enabled on the List RSS settings page

Although publishing RSS feeds using the List RSS Feed functionality is very easy it lacks flexibility. First of all it’s bound to a single list only. Additionally the rendering of the feed is fixed so you cannot customize it to your specific needs. The good news is that an RSS feed published using List RSS Feed is accessible by anonymous users so it’s definitely something you could take into account for simple scenarios on your Internet-facing website.

Publishing RSS feeds using the Content Query Web Part

In most projects you can’t really predict the future of your content and have to take some flexibility into account. This is why it’s generally a good idea to use the Content Query Web Part for publishing RSS feeds. The CQWP allows you to aggregate content from multiple sources within the Site Collection and, because it’s XSLT-based, it allows you to customize the generated RSS feed.

Publishing an RSS feed with the Content Query Web Part is very easy. Assuming you have configured the CQWP for aggregating content, all you have to do is to edit the CQWP, and in the Presentation section under Feed enable the feed for the CQWP. Optionally you can provide a title and a description for the feed.

Feed settings in the Content Query Web Part

When you apply the settings, the CQWP will automatically render an RSS link, which you can use to access the RSS feed.

RSS feed link rendered by the Content Query Web Part after enabling RSS

RSS feed published by the Content Query Web Part

Although the CQWP has published an RSS feed, it is missing some information. In my sample articles I’ve included author’s name using the Byline field and the article date using the Article Date field. Let’s see how to get those fields included in the CQWP RSS.

Configuring Content Query Web Part RSS content

The SharePoint 2010 Content Query Web Part has a number of improvements comparing to its previous version, among which are slots. Slots are the new way of binding data to the Item Style in the CQWP which is pretty convenient since it saves you from editing hidden CQWP properties like CommonViewFields and DataColumnRenames like you had to in MOSS 2007 in order to include custom fields in your content aggregation. An even better news is that the RSS feed in CQWP is yet another XSL template which also uses slots to map data to the RSS markup.

In order to bind data to the RSS feed we first need to know which slots the CQWP RSS uses. Looking in the web.config of your Web Application you can find out that the CQWP uses the /Style Library/Xsl Style Sheets/Rss.xsl file for rendering RSS feeds.

CQWP RSS XSLT setting highlighted in the web.config

By examining this file you can see that the default CQWP RSS template contains the following slots: Title, LinkUrl, Description, PubDate, FileExtension, FileSize, ID, ListId, WebId. The good news is that most of those slots are already mapped by the standard CQWP Item Styles or by the CQWP itself so you don’t have to worry about them.

In order to map the article date we have to ensure that our Item Style template contains a slot called PubDate. As for the author things are a little more complicated. The default RSS XSLT doesn’t include author information, so in order to do so you would either have to customize the standard RSS XSLT or deploy your own. In this example we will use the easiest approach and we will customize the existing Rss.xsl file.

Extending the Item Style template with new slots

Assuming that the CQWP is already working, it means that you already have a fully functional Item Style that does and looks exactly like it should. However, in order to include some additional data in the RSS feed, your existing Item Style has to be extended with new slots. The great thing is, that you don’t necessary have to display the data mapped to the new slots. As long as the mapping between the slot and the data exists, the CQWP will apply the mapping to the template.

Let’s add two new slots to our Item Style template: one for the article date and one for the author. Add the following snippet to your Item Style template:

<xsl:variable name="HiddenSlots">
    <xsl:value-of select="@Author"/>
    <xsl:value-of select="@PubDate"/>
</xsl:variable>

After saving the changes you should see the slots appear in the CQWP properties:

The author and article date slots added to the CQWP

Now we have the slots in place the last step is to map them to content from our aggregation:

Slots mapped to data from content aggregation

Although our current Item Style template doesn’t show any changes the CQWP has made the mappings and that content should appear in the RSS feed. In fact if you open the RSS feed you should see the value of the pubDate element change from the Created field to our Article Date field:

RSS with the standard pubDate replaced with the article date

Extending the CQWP RSS XSL with additional information

As I mentioned before by default the CQWP RSS doesn’t include author information. However, since the RSS feed is based on an XSLT stylesheet, we can easily extend it to make our CQWP include some additional information in the RSS feed. In this example I will show you how to customize the existing Rss.xsl file.

Go to SharePoint Designer and open from the Style Library the Rss.xsl file. In line 21 add the dc namespace to the rss element:

xmlns:dc="http://purl.org/dc/elements/1.1/"

dc namespace added to the rss element

Next move down to line 49 and add the following snippet:

<xsl:if test="string-length(@Author) != 0">
    <dc:creator><xsl:value-of select="@Author"/></dc:creator>
</xsl:if>

Author information added to the RSS XSLT

This snippet will include author information in the RSS feed only if it has been set.

When you save the changes and reload the XSLT you should see the author information included in the RSS feed:

Author information included in the RSS feed

Summary

SharePoint 2010 provides you a few mechanisms for publishing RSS feeds to your visitors. In most scenarios it is the best approach to use the Content Query Web Part for publishing RSS feeds since it’s flexible. Although the standard CQWP RSS feed doesn’t include all the RSS information by default, it can be extended easily to provide a more complete experience to the readers without using custom code.

Technorati Tags: SharePoint 2010

Others found also helpful: