Aggregating RSS feeds from Umbraco content

Serving Umbraco website content in RSS format

  • Share

RSS stands for Really Simple Syndication. It's an easy way for you to keep up with news and information that's important to you, and helps you avoid the conventional methods of browsing or searching for information on websites.

Umbraco stores it's published content in XML format, so exposing website data is really easy. Idealy you can use XSLT to just transform data, but because it is so ancient and hard to debug and maintain we are going to use Syndication classes already built in Umbraco and Web API to expose RSS XML format.

Web API is really good because it recognizes client accept type and based on that it renders out different format. This is very useful if you have different clients, but in our case we just want to get some XML and it always be XML regardless of the type of client. Please refer to Forcing accept type for WebApi call article for more about forcing output type from web service.

First thing we have to do is to create a controller which will expose Web API service entry point.

using System.Text;
using System.Web.Http;
using System.ServiceModel.Syndication;
using Umbraco.Core;
using Umbraco.Web;
using Umbraco.Core.Models;
using Umbraco.Web.WebApi;
using Umbraco.Cms.Custom.Extensions;
using Umbraco.Cms.Custom.Common;
using System.Web;
using System.ServiceModel.Web;
using Umbraco.Cms.Custom.ApiFilters;

namespace Umbraco.Cms.Custom.Controllers
{
    public class RssFeedController : UmbracoApiController
    {
        [HttpGet]
        [ReturnXmlOnly]
        public Rss20FeedFormatter GetFeed()
        {
		
		}
	}
}
    

Filter attribute ReturnXmlOnly is mentioned in article Forcing accept type for WebApi call so you can get its code from this article.

The next step is to generate feed content. For that purpose I used System.ServiceModel.Syndication.SyndicationFeed class. Basically feed instance is filled with the data from Umbraco published content using Umbraco 6 API and at the end instance is parsed with Rss20FeedFormatter.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Http;
using System.ServiceModel.Syndication;
using Umbraco.Core;
using Umbraco.Web;
using Umbraco.Core.Models;
using Umbraco.Web.WebApi;
using Umbraco.Cms.Custom.Extensions;
using Umbraco.Cms.Custom.Common;
using System.Web;
using System.ServiceModel.Web;
using Umbraco.Cms.Custom.ApiFilters;

namespace Umbraco.Cms.Custom.Controllers
{
    public class RssFeedController : UmbracoApiController
    {
        [HttpGet]
        [ReturnXmlOnly]
        public Rss20FeedFormatter GetFeed()
        {
            
            SyndicationFeed feed = new SyndicationFeed();
            IPublishedContent articleContainer = Umbraco.TypedContentAtRoot().First();
            if (articleContainer != null)
            {
                List<SyndicationItem> items = new List<SyndicationItem>();

                foreach (IPublishedContent article in articleContainer.Descendants(5).Where(a => a.DocumentTypeAlias == "Article"))
                {
                    SyndicationItem item = new SyndicationItem(
                        article.GetPropertyValue("title").ToString(),
                        Methods.GetBriefContent(article),
                        new Uri(article.UrlWithDomain()));

                    item.Id = article.Id.ToString();
                    item.PublishDate = article.CreateDate;
                    item.LastUpdatedTime = article.CreateDate;
                    item.Categories.Add(new SyndicationCategory(article.AncestorOrSelf(2).GetPropertyValueAsString("title")));
                    items.Add(item);
                }

                feed.Items = items.OrderByDescending(i => i.PublishDate);

            }

           
            return new Rss20FeedFormatter(feed);
        }
    }
}
    

The rest is on webservice to send back the data in XML format.

This actual code is used on this blog to generate RSS feed from blog articles.

References

  • Share

Disclaimer

Purpose of the code contained in snippets or available for download in this article is solely for learning and demo purposes. Author will not be held responsible for any failure or damages caused due to any other usage.

Comments for this article

comments powered by Disqus