Second Life of a Hungarian SharePoint Geek

January 21, 2010

Using the Content Query Web Part to provide an RSS feed on a publishing site

Filed under: CQWP, SharePoint — Tags: , — Peter Holpar @ 00:48

CQWP that is part of the publishing infrastructure feature in MOSS 2007 provides an excellent way of aggregating information on the site. This web part has the capability to yield its content as an RSS feed.

You can customize the visual styles of the items using the XSL files, but what happens if you don’t want to display the items on the web user interface at all, and need the RSS feed only?

In this case we can create a custom web part that is derived from the CQWP, and override the Render method to display a link to the RSS feed. The feed of the CQWP is provided by the feed.aspx and we should pass a few query string parameters to this page to specify exactly the CQWP that should be used when creating the feed. These parameters help to identify the web, the page and the web part itself.

The following code block illustrates a sample implementation of the web part.

  1. using System;
  2. using System.Web.UI;
  3. using System.Web.UI.WebControls.WebParts;
  4. using Microsoft.SharePoint;
  5. using Microsoft.SharePoint.WebControls;
  6. using Microsoft.SharePoint.WebPartPages;
  7. using Microsoft.SharePoint.Publishing.WebControls;
  8. using Microsoft.SharePoint.Utilities;
  9.  
  10. namespace RSSWebPart
  11. {
  12.     public class RSSWebPart : ContentByQueryWebPart
  13.     {
  14.         protected override void Render(HtmlTextWriter writer)
  15.         {
  16.             try
  17.             {
  18.                 // RSS feed must be enabled for the web part
  19.                 if (FeedEnabled)
  20.                 {
  21.                     SPWeb web = SPContext.Current.Web;
  22.                     SPFile file = SPContext.Current.File;
  23.                     writer.Write(@"<a href='/_layouts/feed.aspx?xsl=1&web={0}&page={1}&wp={2}' style=""padding:3px;margin:0;""><img src='/_layouts/images/rss.gif' border='0' alt=""RSS""  />",
  24.                         SPEncode.UrlEncode(web.ServerRelativeUrl), file.UniqueId, StorageKey);
  25.                     if (!String.IsNullOrEmpty(FeedTitle))
  26.                     {
  27.                         writer.Write(@"<span style=""padding:0;margin:0;vertical-align:top;""><b>{0}</b></span><br /></a>",
  28.                             SPEncode.HtmlEncode(FeedTitle));
  29.                     }
  30.                     if (!String.IsNullOrEmpty(FeedDescription))
  31.                     {
  32.                         writer.Write("{0}<br />", SPEncode.HtmlEncode(FeedDescription));
  33.                     }
  34.                 }
  35.             }
  36.  
  37.             catch (Exception ex)
  38.             {
  39.                 // log the error here
  40.             }
  41.         }
  42.     }
  43. }

After deploying the web part we add a new instance of it to a page, and set the feed properties in the CQWP settings as illustrated below.

image

We also set the chrome type to None.

image

The next figure shows the result of the process.

image

The RSS feed will be opened by clicking on the feed title or on the RSS icon.

If you need more information about configuring the RSS support for a CQWP you will find a useful article about that here.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: