Creating RSS feeds with Umbraco

It's not very often that I have to set up a RSS feed, so when times come, and I actually have to, I have most likely forgotten how the XML should be structured, what mime type to return and so on. Therefore I decided to create the package Skybrud.Umbraco.RssUtils so I (and you) can setup RSS feeds with ease.

The package contains just two classes, so I'll start out with explaining how to use these.

The basics

The main class is called RssFeed, and as the name suggests, it describes your RSS feed. The class has several constructors to better suit specific needs. An instance of RssFeed contains instances of RssItem, but depending on your use case, you most likely won't have to use this class directly.

One of these constructors has three parameters: a string containing the title of the feed, another string containing a link to the site or the feed itself and last an instance of IPublishedContent. This instance is the parent node of the nodes to be added as RSS items.

So considering you have a blog containing a number of blog posts, you can set up a RSS feed something like this:

// Get the root node of the blog
IPublishedContent blog = Umbraco.TypedContent(1234);

// Initialize a new feed and add all blog posts
RssFeed feed = new RssFeed("Bjerner.dk Blog", "http://www.bjerner.dk/blog/", blog);
    
// Write the RSS feed to the response stream
feed.Write();

This example will add all child nodes under blog - even if this is hundreds of blog posts. Normally it is best practice only to have the most recent items - eg. the 30 most recent blog posts. This can be done using another constructor, which rather than taking a single instance of IPublishedContent, takes an instance of IEnumrable<IPublishedContent>. So we could do something like this:

// Get the root node of the blog
IPublishedContent blog = Umbraco.TypedContent(1234);

// Take the first 30 children
IEnumerable<IPublishedContent> blogPosts = blog.Children.Take(30);

// Initialize a new feed with the 30 most recent blog posts
RssFeed feed = new RssFeed("Bjerner.dk Blog", "http://www.bjerner.dk/blog/", blogPosts);
    
// Write the RSS feed to the response stream
feed.Write();

When items are added through the constructor, they are automatically sorted by their creation date and in descending order so that the newest blog post is on the top.

Calling the Write method will write the proper XML to the response stream as well as setting the application/rss+xml mime type.

The XML

Since my blog currently only has two blog posts, both of the above examples will generate some XML roughly looking like this:

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
<?xml version="1.0" encoding="utf-8"?>
<rss>
  <channel>
    <title>Bjerner.dk Blog</title>
    <link>http://bjerner.dk/blog/</link>
    <pubDate>Wed, 07 May 2014 14:56:22 GMT</pubDate>
    <item>
      <title>Creating RSS feeds with Umbraco</title>
      <link>http://bjerner.dk/blog/creating-rss-feeds-with-umbraco/</link>
      <pubDate>Thu, 08 May 2014 11:45:15 GMT</pubDate>
      <guid>4067</guid>
    </item>
    <item>
      <title>Working with media in Umbraco with multiple environments</title>
      <link>http://bjerner.dk/blog/working-with-media-in-umbraco-with-multiple-environments/</link>
      <pubDate>Thu, 19 Dec 2013 12:36:41 GMT</pubDate>
      <guid>2064</guid>
    </item>
  </channel>
</rss>

Taking it a little further

The two examples above will convert the blog items directly into RSS items and map the corresponding properties where needed (which is good in most scenarios). But lets assume our blog posts have a specific property for the publication date and that we for some reason want to set the GUID as the MD5 encoded/hashed value of the ID rather than just the ID itself. Yet another constructor lets us specify a delegate for converting an instance of IPublishedContentto an instance of RssItem.

// Get the root node of the blog
IPublishedContent blog = Umbraco.TypedContent(1234);

// Take the first 30 blog posts
IEnumerable<IPublishedContent> blogPosts = blog.Children.Take(30);

// Initialize a new feed and add the blog posts
RssFeed feed = new RssFeed("Bjerner.dk Blog", "http://bjerner.dk/blog/", blogPosts, delegate(IPublishedContent content) {
    return new RssItem {
        Title = content.Name,
        Link = content.UrlWithDomain(),
        PubDate = content.GetPropertyValue<DateTime>("publishDate"),
        Guid = MD5Encode(content.Id + "")
    };
});

// Write the RSS feed to the response stream
feed.Write();

That's it. The classes have some further options which I will try to document at a later time, but for now you can grab the package via NuGet or find the source code at GitHub.

The code is currently only tested in Umbraco 7.0.4, and should be considered somewhat beta'ish at the moment.