(If you are wondering what this blogpost is about, please read my series on Sharepoint Advanced Webparts)
Before I cause any confusion, AS stands for Advanced Scenarios !!
So yesterday I published an outline giving a fair overview of what to expect out of this series. I talked about writing a web based RSS reader, a.k.a. bloglines or newsgator. I mentioned that we will need atleast two WebParts.
a) Something that lets you manage your OPML
b) Something that displays a particular RSS feed.
So let us go ahead and write the WebParts next. Now this series will not cover basics. If you must read about the basics of writing a webpart, I suggest reading this blogpost up - "Writing Custom Web parts for SharePoint 2007". In this blogpost, I am going to go ahead and write the WebPart that displays an RSS Feed, awrite !!
1. Writing the WebPart that displays a particular RSS Feed
So the first thing I am going to do is, set up the business objects necessary for my webpart. You can see them in the below class diagram.

As you can see, I have written up two simple objects - one to represent an RssFeed, and the other to represent a singular item in that feed.
The code for these business objects simply accepts the relevant constructors and sets the appropriate properties.
Here is the listing for RssFeed -
internal class RssFeed : List<RssItem>
{
private XmlDocument rssDoc;
internal RssFeed(string RssURL)
{
try
{
rssDoc = new XmlDocument();
XmlTextReader xRead = new XmlTextReader(RssURL);
rssDoc.Load(xRead);
XmlNodeList xNodes = rssDoc.SelectNodes("./rss/channel/item");
foreach (XmlNode xNode in xNodes)
{
this.Add(new RssItem(xNode)) ;
}
}
catch (Exception)
{
this.Add(new RssItem()) ;
}
}
}
And here is the listing for RssItem -
internal class RssItem
{
private string title;
private string href;
private string body;
public string Href
{
get { return href; }
}
public string Title
{
get { return title; }
}
internal RssItem()
{
title = "Feed not available at this time" ;
href = "~" ;
}
public string Body
{
get { return body; }
set { body = value; }
}
internal RssItem(XmlNode xNode)
{
title = xNode.SelectSingleNode("./title").InnerText;
href = xNode.SelectSingleNode("./link").InnerText;
body = xNode.SelectSingleNode("./description").InnerText;
}
}
As you can see, I am simply relying on the fact that the 3 necessary elements per the RSS2.0 RFC - title, link and description are present.
Now with my business objects setup, my WebPart that renders the RSS Feed becomes dead simple to implement. The code for my WebPart can be seen as below.
public class RssRender : WebPart
{
private string rssUrl;
private string feedName;
protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
{
RssFeed feed = new RssFeed(rssUrl);
foreach (RssItem singleRssItem in feed)
{
writer.Write("<div class=\"container\">");
writer.Write("<h2>");
writer.Write(singleRssItem.Title);
writer.Write("</h2>");
writer.Write("<p class=\"teaser\">") ;
writer.Write(singleRssItem.Href) ;
writer.Write("</p>") ;
writer.Write("<div class=\"module-content\"><table width=\"100%\" border=\"0\"><tr><td>");
writer.Write(singleRssItem.Body);
writer.Write("</td></tr></table></div>");
writer.Write("</div>") ;
}
}
public string FeedName
{
get
{
return feedName ;
}
set
{
feedName = value;
}
}
[WebBrowsable(true)]
[Personalizable(true)]
public string FeedURL
{
get
{
return rssUrl;
}
set
{
rssUrl = value;
}
}
}
Great !! With this setup, let us fire up our WebPart framework. I am going to fire up SpareJoint - which is a framework I wrote (will release it soon as open source, promise! promise! :), but you can write one up real quickly yourself).
So when I fire up the home page, I see a blank page as shown below -

I am going to go ahead and log-in to my framework, and go ahead and pick the RssRender WebPart we just wrote from the catalog, and add an instance to a WebPartZone I have setup in the middle of the page. This can be seen as below -

For the keen eyed, I also went ahead and set my page in "Edit" display mode, and I am going to go ahead and click "Edit" and specify the various necessary properties. Once in EditMode, I am going to change the "Chrome Type" to "none", and change the FeedURL to http://blah.winsmarts.com/rss2.aspx . This is shown as below.

Go ahead and click "OK", and then "Logout" (in case of SpareJoint because SpareJoint is intended to set the page in Browse DisplayMode when you hit LogOut).
You should now see the page in full glory as shown below - (LOL, Look Familiar???)

Wonderful !! :). I can now show RSS. (Sharepoint 2007 does ship with a WebPart that displays RSS, but it's communication seems to be broken, plus writing your own is so much more fun anyway :-/).
In my next blogpost, I will write up a quick webpart that has the ability to let you hold your OPML.
Cool huh? Stay Tuned!!
Thou shalt - 