Generating RSS Feeds for SharePoint Sites
Today I have a day off, so I decided to work a little bit on my “pet project”: RSS generation for SharePoint sites. Scoble asked for it when I released my RSS Reader Webpart. I know there are some good solutions for this problem available (for example from Siegfried Weber and DevHawk), but I was intrigued by this problem so I decided to craft one myself. Et voila: the What’s New Rss Feed for SharePoint was born (see the end of the post for downloads). I’ve re-used to logic of the What’s New Web Part to retrieve the latest changes on a SharePoint site so I won’t go into detail about that part of the code. The general idea behind this is that the code will go through all the lists on a SharePoint site (you can exclude specific lists) and retrieves the latest changed items (you can specify how many).
An APSX page will build the RSS Feed (more details the actual RSS generation later on) and will use the SharePoint object model to access the contents of a specific SharePoint site. The tricky part here is how the ASPX page can access the SharePoint object model. This issue is solved by deploying the ASPX page to the _VTI_BIN directory of a SharePoint site. You may have noticed this, but this directory is kind-a special: this directory is accessible from each SharePoint site, e.g.: http://sharepoint.leadit.be/_vti_bin/... , http://sharepoint.leadit.be/SubSite/_vti_bin/... . It doesn’t matter which URL you use, you’ll always end up in the same virtual _VTI_BIN directory. To find out where this magical virtual directory maps to on your hard disk, you can use the IIS Manager: navigate to the _VTI_BIN directory and check the properties. The default for the Local Path property is “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\60\isapi”. So we need to deploy the ASPX page to this directory and of course the corresponding assembly to \BIN in this directory.
The next problem to solve is how to get a hold of the SharePoint web object we want to access. Actually this is quite simple: the GetContextWeb function of the SPControl class can be used to get a SPWeb instance. Based on the Context we’re in, we’ll get the corresponding SPWeb instance.
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
SPWeb web = SPControl.GetContextWeb(Context);
To construct the contents of the RSS Feed, I’ve used the open source RSS.NET library. This library allows you to build an RSS Feed through a very nice object model. When you’re done you can write the contents to the Response stream:
RssFeed rssFeed = new RssFeed();
rssFeed.Channels.Add(rssChannel);
Response.ContentType = "text/xml";
Response.ExpiresAbsolute = DateTime.MinValue;
rssFeed.Write(Response.OutputStream);
To deploy the APSX page, you only have to copy WhatsNewRss.aspx to the directory that maps to the _VTI_BIN virtual directory (probably C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\60\isapi) and Leadit.SharePoint.Services.dll to the corresponding BIN directory (probably C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\60\isapi\BIN). Now you should be able to browse to the ASPX page through a SharePoint URL, for example http://sharepoint.leadit.be/_vti_bin/WhatsNewRss.aspx. There are three parameters (which you can add to the URL) that you can use to alter the contents of the feed (similar to the properties of the What’s New Web Part):
- ExcludeLists
Specify which lists you want to exclude, separated by a semi-colon (;).
Default value: Web Part Gallery;fpdatasources - ListFields
Specify what field for a specific list should be used as a title.
Default value: Contacts=Full Name;Links=URL - ItemsToDisplay
Specify how many items to display.
Default value: 15
Example:
http://localhost:8000/_vti_bin/WhatsNewRss.aspx?ItemsToDisplay=5&ExcludeLists=MyList&ListFields=Contacts=Last Name
The result is an RSS Feed which you can view in any feed reader, for example SharpReader:
As always, please test before you put this thing in production (remember this is V1). Let me know if you have any problems! One final note: for some feed readers it’s necessary to alter the Directory Security for the _VTI_BIN virtual directory. To access the feed from these feed readers you need to allow Basic Authentication:
Downloads: