Sharepoint Webparts AS – Writing the WebParts - The RSS Feed WebPart

Posted on 7/14/2006 @ 5:55 AM in #SharePoint by | Feedback | 23193 views

(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 -

Sound off but keep it civil:

Older comments..


On 7/11/2006 7:09:21 AM Jun Meng said ..
It looks easy to write webpart for Sharepoint 2007. :)

Does your webpart support multiple RSS feeds?


On 7/11/2006 7:57:13 AM Sahil Malik said ..
Hey Jun,

Well thats the point - it doesn't need to :). Next I will write an OPML webpart - which holds all your RSS feeds. With it's help you could work with multiple RSS feeds in the above webpart. It's comin' next :)

SM


On 4/17/2007 1:37:40 PM Magnus Sälgö said ..
I wrote an basic article about how I handöe errors in Sharepoint and got a complain that the article was to simple....

Question: Have you seen any best practise how to handle errors inside a webpart in Sharepoint 2007

http://www.codeproject.com/useritems/Errorhandling_in_Webparts.asp


On 12/10/2007 4:04:36 AM Chander said ..
Hi Sahil,Does your RSS Reader webpart have the ability for xslt styling?

Thanks

On 4/2/2008 11:44:25 PM John said ..
Do you have a web part that acts as a timezone for al countries in the world? If not all, maybe


some?

Thanks,


John


On 4/23/2008 3:08:36 PM Veysel said ..
Thanks! This was exactly what I was looking for!


On 11/5/2009 12:51:24 PM Ram said ..
hi Sahil,


Did you came up with multiple RSS feed for MOSS ?


I need it. i found one in codeplex but its not working.


Let me know if you have any other links.

Thanks, Ram


On 12/1/2009 2:53:35 PM May said ..
How can I show rss subscriptions of individual users in a webpart?


On 11/4/2010 7:31:34 AM Sreenivas said ..
HI,

I tried doing the same.But when i gave the http://blah.winsmarts.com/rss2.aspx in the feed URL property it is showing "Feed not available at this time".Then i tried debugging to figure the problem.The problem is in the RSSFeed.cs line "XmlTextReader xRead = new XmlTextReader(RssURL);" causing exception.in xRead value is getting null.Could any one please help me on this?

Thanks,


Sreeni