Wednesday, April 1, 2009

Programmatically accessing RSS Feed using RSSToolkit Control

An awesome RSS Feed control (Dmitry’s RSSToolkit) was released years before, I came to knew about ASP.NET.

Last months, I’s spending some time with the control.  I must say, its really cool. One problem I encountered; lack of documentation (or my lack of knowledge), What ever it may. But, it was really nice to write some cool codes using the control.

Ok! In this article, I’ll show small piece of codes demonstrating how to retrieve RSS Feeds from sites programmatically.

Before we start, please download RSSToolkit 2.0 from codeplex, and add the control to your Visual Studio toolbox (?).

Step 1: Drag-drop ‘RssDataSource’ control from your toolbox to the webpage.

Choose RSSDataSource

Now just keep an eye on the source-view. When you drag-drop the RssDataSource, you’ll get a Register tag in the source-view like:

<%@ Register assembly="RssToolkit" namespace="RssToolkit.Web.WebControls" tagprefix="cc1" %>

Step 2: Drag-drop a TextBox for  accepting the feed-url.

Step 3: Drag-drop a Button to trigger fetching the feeds. Double-Click the button to generate the Click event.

Step 4: Drag-drop a Label control to show an error when you enter an invalid feed-url.

[Design-View]

Design-View

[Source-View]

<body>
<form id="form1" runat="server">
<div>
<cc1:RssDataSource ID="RssDataSource1" runat="server">
</cc1:RssDataSource>
<br />
Enter Feed URL:
<asp:TextBox ID="TextBox1" runat="server" Width="300px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Fetch" />
<br />
<asp:Label ID="Label1" runat="server" Font-Bold="True" ForeColor="#FF3300"></asp:Label>
</div>
</form>
</body>

[Code-View]

using RssToolkit.Rss;

// Fecth RSS
protected void Button1_Click(object sender, EventArgs e)
{
string feedData = string.Empty;
// Accepts Feed URL
RssDataSource1.Url = TextBox1.Text.Trim();

try
{
// Iterate through the RSS feed Items
foreach (RssItem item in RssDataSource1.Rss.Channel.Items)
{
// Feed Author
feedData = item.Author;
// Feed Title
feedData = item.Title;
// Feed Link
feedData = item.Link;
// Feed Description
feedData = item.Description;
// Feed Publish Date
feedData = item.PubDate;

// Print the output
Response.Write(feedData + "<br/><hr/><br/>");
}
}
catch (UriFormatException exp)
{
Label1.Text
= "Invalid Feed URL";
}
}

[Demonstration]
a) copy the url of an RSS feed, say http://feeds2.feedburner.com/DidYouSaynet.

b) Paste it on the TextBox & Hit the button. That's all.

c) See How the feeds are displayed.

Hoping it will help someone.

Note:

a) Do not forget to include the namespace using RSSToolkit.RSS.

b) One of the common question you may encounter is how to fetch feeds with a count limit?. That means you only want to fetch last 3 feeds, 5 feeds, etc. At this point I don't have the answer. Even though the RSSDataSource has a property named MaxItems ( where you can set the feed limit), it was not working when tried programmatically. Only way I believe you can do; is to insert a count value in the foreach statement  & break the loop when you feed-count limit is reached.

c) Next question will be how can I download updated feeds only from a site?. The answer will be my next post.

Also you can find some cool articles of RSSToolkit @ ScottGu’s blog and @ CodeProject, which inspired me to write this.

Thanks for taking time to read the post.

2 comments:

Exile said...

Waiting on your next post. :)
Thanks for this post.

Abhilash said...

Hi,
Good to see that you liked the post. Sure, post about fetching the updated feeds once I get enough time. Thanks.

 
Best viewed in Internet Explorer 8.