Friday, August 14, 2009

RSS Aggregator Webpart using Syndication

Scenario:
I read about Syndication sometime back after getting some hint from my friend Srini. RSS is now main stream requirement for getting updates from application / blogs and other sources. Microsoft always keep enhancing the dot net framework to make it more richer so they added support for syndication with dot net framework 3.5

Solution:
Here's a small webpart on how to use Syndication for reading RSS feeds from any site.

Steps:
1. Make sure you are using Framework 3.5
2. You need to add reference to System.ServiceModel.Web.dll

RSS WP

Code:

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.ServiceModel.Syndication;
using System.Web.UI;
using System.Xml;

namespace SyndicationWP
{
public class RSSWP : System.Web.UI.WebControls.WebParts.WebPart
{
protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);

XmlReader reader = XmlReader.Create("http://feeds.feedburner.com/sandeepnahta");

SyndicationFeed feed = SyndicationFeed.Load(reader);
writer.WriteLine(feed.Title.Text);
writer.WriteLine("Items:");
foreach (SyndicationItem item in feed.Items)
{
writer.WriteLine("Title: {0}", item.Title.Text);
writer.WriteLine("Summary: {0}", ((TextSyndicationContent)item.Summary).Text);
}
writer.WriteLine("Press <enter> to quit...");
}
}
}
Possible Errors:
401 Unauthorized error if you are trying to consume a feed which you are not authorized to access. Most of sharepoint list RSS feed are protected.

Possible Improvements:
1. It can be generalized for any RSS feed links

Article:
SyndicationFeed Class

0 comments: