What is this post about?

If you have a blog, or a site with news articles, or even a podcast site, you will probably need to have an RSS feed set up.

And if you use Umbraco CMS then this is the post for you. In this post I give you a simple template to start using for your own feed. It's very simple and straight forward. It should be a simple case of copy > paste > edit > save > job done.

What do I need to do?

  • Just copy this code and paste it into a new template.
  • Edit the values at the top to be relevant to your site.
  • Create a doc type which uses the new template.
  • Allow this new doc type under the home page and create a page using it, called '/feed' or '/rss'
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    Layout = null;
    Response.AddHeader("Content-Type", "text/xml");

    const string DATE_FORMAT = "ddd, dd MMM yyyy hh:mm:ss zzz";
    const string FEED_TITLE = "codeshare.co.uk Blog";
    const string FEED_DESCRIPTION = "codeshare.co.uk Blog | Code examples and knowledge sharing";
    const string CREATOR_NAME = "Paul Seal";
    const string CATEGORY = "Web Development";
    const string UPDATE_PERIOD = "daily";
    const int UPDATE_FREQUENCY = 1;
    const string LANGUAGE = "en-US";
    const string HOME_PAGE_DOC_TYPE_ALIAS = "home";
    const string FEED_PARENT_DOC_TYPE_ALIAS = "blog";
    const string ARTICLE_DOC_TYPE_ALIAS = "blogPost";
    const string ARTICLE_TITLE_PROPERTY_ALIAS = "pageTitle";
    const string ARTICLE_DATE_PROPERTY_ALIAS = "blogPostDate";
    const int CONTENT_PREVIEW_LENGTH = 500;

    IPublishedContent homePage = Model.Content.AncestorOrSelf(1).DescendantsOrSelf().Where(x => x.DocumentTypeAlias == HOME_PAGE_DOC_TYPE_ALIAS).FirstOrDefault();
    IPublishedContent feedParentPage = homePage.Descendants().Where(x => x.DocumentTypeAlias == FEED_PARENT_DOC_TYPE_ALIAS).FirstOrDefault();
    IEnumerable<IPublishedContent> feedItems = feedParentPage.Descendants().Where(x => x.DocumentTypeAlias == ARTICLE_DOC_TYPE_ALIAS && x.IsVisible()).OrderByDescending(x => (DateTime)x.GetPropertyValue(ARTICLE_DATE_PROPERTY_ALIAS));
    DateTime lastBuildDate = feedItems.Max(x => x.UpdateDate);
    string siteUrl = homePage.UrlWithDomain();
    string feedUrl = ((IPublishedContent)CurrentPage).UrlWithDomain();

<rss version="2.0"

        <atom:link href="@feedUrl" rel="self" type="application/rss+xml" />
        @foreach (IPublishedContent item in feedItems.OrderBy(ARTICLE_DATE_PROPERTY_ALIAS + " desc"))
            string articleDescription = Umbraco.Truncate(umbraco.library.StripHtml(item.GetGridHtml("contentGrid", "bootstrap3").ToString()), CONTENT_PREVIEW_LENGTH).ToString().Replace("&hellip;", "...");
                <title>@(item.HasProperty(ARTICLE_TITLE_PROPERTY_ALIAS) ? item.GetPropertyValue<string>(ARTICLE_TITLE_PROPERTY_ALIAS) : item.Name)</title>
                <guid isPermaLink="false">@item.UrlWithDomain()</guid>

Need a video to help you follow the instructions?

Have a look at this video where I try to implement the sample code on the default starter site you get with Umbraco.
If you want to be notified when I post new videos, click on subscribe in my author block below this post.

Are you stuck?

If you are stuck, please don't hesitate to ask your question in the comments.

Want to thank me?

If I've helped you out and you want to thank me, why not buy me a coffee?

Buy me a coffee

About the author

Paul Seal

Umbraco MVP and .NET Web Developer from Derby (UK) who specialises in building Content Management System (CMS) websites using MVC with Umbraco as a framework. Paul is passionate about web development and programming as a whole. Apart from when he's with his wife and son, if he's not writing code, he's thinking about it or listening to a podcast about it.

Related Posts

How to solve the error assets file project.assets.json not found in Visual Studio

This post tells you how to solve the error assets file project.assets.json not found in Visual Studi…

Read Post

Code to help you debug an umbraco issue on a remote site

This post gives you some razor code to help you see the values of the IPublishedContent item's prope…

Read Post

How to fix the 403 error on preview in Umbraco after upgrading

This post shows you how to fix the 403 error on preview after you have upgraded it.

Read Post

How to restrict Content Apps by user groups in Umbraco

In this post I give you some code so you can restrict the Content Apps on your Umbraco site by user…

Read Post