Welcome to Dovetail Software Blogs : Sign in | Join | Help
Creating RSS Feeds Using ASP.Net

Warning: This is one of those late to the party posts where I show off some cool thing that everyone already likely knows about. I just feel special right now because I was able to push out something we’ve wanted to do for quite some time in an afternoon.

We wanted to have an RSS feed so customers could keep up to date with new or recently updated Dovetail Knowledge Base articles. My personal mental gate was that I really didn’t want to try to figure out how to properly create an RSS or Atom feed and I didn’t want to bother with complex heavy handed frameworks like Argotic. Then one day Josh introduced me to the System.ServiceModel.Syndication namespace (introduced in .Net 3.5) and my life was never the same.

Simple stuff really. Create a SyndicatedFeed filled with feed items and hand that to a Feed Formatter.

Generating an RSS feed

public void ProcessRequest(HttpContext context)
{
    SyndicationFeed feed = CreateRecentSolutionsFeed();
 
    var output = new StringWriter();
    var writer = new XmlTextWriter(output);
 
    new Rss20FeedFormatter(feed).WriteTo(writer);
 
    context.Response.ContentType = "application/rss+xml";
    context.Response.Write(output.ToString());
}

But I get ahead of myself. We have to serve this feed from somewhere. Not really being an ASP.Net expert Josh got me started with a Generic Handler.

image

Which seems a thin construct for simply spiting content out of a URL. Perfect.

A Syndication Feed

To build the syndication I created this little helper method.

 
private static SyndicationFeed CreateRecentSolutionsFeed()
{
    var syndicationItems = GetRecentOrModifiedSolutionSyndicationItems(TimeSpan.FromDays(30));
 
    return new SyndicationFeed(syndicationItems)
               {
                   Title = new TextSyndicationContent("Dovetail Software Knowledge Base"),
                   Description = new TextSyndicationContent("Recently created or modified solutions regarding products offered by Dovetail Software."),
                   ImageUrl = new Uri("http://www.dovetailsoftware.com/images/header_logo.gif")
               };
}
Assembling a Syndication Item

I’ll skip the gory Dovetail SDK data access code that goes out to the Clarify database and materializes *poof* any solutions that have been created or modified in the last 30 days. Unless, of course, one of my 2 readers asks nicely for it. The interesting bit in any case is that it basically loops over all the solutions found and assembles a Syndication Item for each one.

private static SyndicationItem AssembleSolutionSyndicationItem(ClarifyDataRow solution)
{
    var id = solution["id_number"].ToString();
    var title = String.Format("[{0}] {1}", id, solution["title"]);
    var content = solution["description"].ToString();
    var url = new Uri(String.Format("http://www.dovetailsoftware.com/resources/solutions/{0}.aspx", id));
 
    return new SyndicationItem(title, null, url) { Summary = new TextSyndicationContent(content) };
}

Results

The end result is a totally hard coded HTTP Handler generating an hopefully useful RSS feed for Dovetail’s customers.

Dovetail KB feed

Next up was to add a meta link to the feed in the html header of our knowledgebase pages. This makes the little RSS icon shows in the address bar of honest and decent hard working web browsers.

<link rel="alternate" type="application/rss+xml" href="/resources/knowledgeBaseFed.ashx" title="Dovetail Knowledgebase Feed"/>
image 
Posted: Friday, February 06, 2009 6:33 PM by kmiller
Filed under: ,

Comments

Chad Myers' Blog said:

This has come up a few times in various forums in the past few weeks, so I’m glad my friend and co-worker

# February 6, 2009 9:15 PM

DotNetShoutout said:

Thank you for submitting this cool story - Trackback from DotNetShoutout

# February 7, 2009 7:13 AM

Mark Nijhof said:

Hi Kevin,

Nice explanation! When I played with it I noticed that it also puts an xml header (?xml version="1.0" encoding="utf-16"?) in there and IE doesn't like that.

-Mark

# February 7, 2009 1:22 PM

Kevin Miller said:

While reviewing what I did to Create an RSS Feed Using ASP.Net . We got worried that overzealous customers

# February 10, 2009 1:13 PM

Gary Sherman said:

Kevin has put together an RSS feed for our most recent knowledgebase articles . The feed can be subscribed

# February 10, 2009 4:07 PM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# February 19, 2009 2:52 AM

Dave Burke said:

Sueetie Aggregate Blog Post List Now Supports RSS

# March 11, 2010 7:53 PM

Sueetie said:

Sueetie Aggregate Blog Post List Now Supports RSS

# March 11, 2010 7:56 PM