Insights

Creating a Custom RSS Feed in Sitecore

A guide for how to customize the built-in RSS feed functionality in Sitecore.

Before Creating a Custom RSS Feed in Sitecore

If you’re just looking to create a quick RSS feed for your own website, Sitecore already has an out of the box solution. Click here to learn more. If you need something that displays more detail, or perhaps integrate with another news site such as Presspage, this is the place to learn how!

Create Your RSS Feed

You can follow this link for a more detailed explanation, or just read below for a quick summary.

  1. Create an item using the /sitecore/templates/System/Feeds/RSS Feed template.
  2. In the Items field, choose the parent folder/item of all the items you want displayed in your RSS feed. You can use a Sitecore query too if you want to be more specific.
  3. Configure the feed. Go to any item you want displayed, and in the top ribbon, go to Presentation, then click Design. A popup will appear and you can fill in the Title, Body, and Date field. All the other items with the same template will follow this configuration as well.
    1. Note: it doesn’t matter what you select, our custom code will overwrite everything.

Customizing Basics

A developer can easily customize the feed by creating a class that extends Sitecore.Syndication.PublicFeed . There’s no adding to the pipeline, or changing configs.

First, let’s start by creating that class, and also overriding the RenderItem method.

using Sitecore.Syndication;
using Sitecore.Data.Items;
using System.ServiceModel.Syndication;

namespace YourProject.RSS
{

    public class CustomRSSFeed : PublicFeed
    {
        //class must be protected!
        protected override SyndicationItem RenderItem(Item item)
        {
            // Don't forget to add null checks
            SyndicationItem syndicationItem = base.RenderItem(item)
            //customizing goes here!

            //return item
            return syndicationItem;
        }
    }
}

Returning Data

Let’s assume that the item you want to customize has the following 4 fields and you want to display all of them! You will notice in the basic feed, you can only display 3 fields.

  • Title
  • Content
  • Copyright
  • Authors (string delimited list)

Here’s how we can do it.

using Sitecore.Syndication;
using Sitecore.Data.Items;
using System.ServiceModel.Syndication;

namespace YourProject.RSS
{

    public class CustomRSSFeed : PublicFeed
    {
        //class must be protected!
        protected override SyndicationItem RenderItem(Item item)
        {
            // Don't forget to add null checks
            SyndicationItem syndicationItem = base.RenderItem(item)

            // It's actually very simple to display the basic fields
            syndicationItem.Title = new TextSyndicationContent(item["Title"], TextSyndicationContentKind.Html);
            // Summary is actually the text inside <description>
            syndicationItem.Summary = new TextSyndicationContent(item["Content"], TextSyndicationContentKind.Html);
            syndicationItem.Copyright = new TextSyndicationContent(item["Copyright"], TextSyndicationContentKind.Html);
            // Let's display the date as well
            syndicationItem.PublishDate = item.Created;

            // Display the author's is a big harder. We need to create SyndicationPerson items.
            string authors = item["Authors"].ToString();
            var authorSplit = authors.Split(',');

            foreach(string author in authorsSplit)
            {
                SyndicationPerson sp = new SyndicationPerson();
                sp.Name = author;
                syndicationItem.Authors.Add(sp);
            }

            //return item
            return syndicationItem;
        }
    }
}

Using The Custom Feed

In your RSS feed item that you created in Sitecore, you can scroll down to find the following section. The first part is your custom code’s namespace, followed by your assembly name.

Creating a Custom RSS Feed in Sitecore

Publish everything, then head over to you RSS’s URL to check it out.

Customizing Data

One of the things you might have noticed is that if you have images, all the URLs are the local one, eg. src="/-/media/test.jpg”. If we were using a third-party such as Presspage to display our RSS feed items, it wouldn’t display the images. We want the src to be [https://www.ourwebsite/-/media/test.jpg](https://www.ourwebsite/-/media/test.jpg) and here’s how we would do it.

using Sitecore.Syndication;
using Sitecore.Data.Items;
using System.ServiceModel.Syndication;

namespace YourProject.RSS
{

    public class CustomRSSFeed : PublicFeed
    {
        //class must be protected!
        protected override SyndicationItem RenderItem(Item item)
        {
            // Don't forget to add null checks
            SyndicationItem syndicationItem = base.RenderItem(item)

            // It's actually very simple to display the basic fields
            syndicationItem.Title = new TextSyndicationContent(item["Title"], TextSyndicationContentKind.Html);
            // We removed the summary here!
            syndicationItem.Copyright = new TextSyndicationContent(item["Copyright"], TextSyndicationContentKind.Html);
            // Let's display the date as well
            syndicationItem.PublishDate = item.Created;

            // Display the author's is a big harder. We need to create SyndicationPerson items.
            string authors = item["Authors"].ToString();
            var authorSplit = authors.Split(',');

            foreach(string author in authorsSplit)
            {
                SyndicationPerson sp = new SyndicationPerson();
                sp.Name = author;
                syndicationItem.Authors.Add(sp);
            }

            // Customizing the summary here!
            // Getting our website URL
            string homeItem = "/sitecore/content/Home"; //replace this with your content root.

            Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("web");
            Item home = master.GetItem(homeItem);

            var options = LinkManager.GetDefaultUrlOptions();
            options.AlwaysIncludeServerUrl = true;

            string link = LinkManager.GetItemUrl(home, options);

            // A regex for searching for <img src="">
            string pattern = @"\<img.+src\=(?:\""|\')(.+?)(?:\""|\')(?:.+?)\>";
            // get the content of our item
            string body = item["Content"];

            MatchCollection matches = Regex.Matches(body, pattern, RegexOptions.Singleline);

            // Loop through the matches and extract the content
            foreach (Match match in matches)
            {
                string imgContent = match.Groups[1].Value;

                // Check if the first character is '/'

                string modifiedImg = "";

                if (!string.IsNullOrEmpty(imgContent) && !imgContent.StartsWith("/"))
                {
                    modifiedImg = "/" + imgContent;
                }

                // Add our website's URL to the current URL
                modifiedImg = link + modifiedImg;

                // Replace the old URL 
                body = body.Replace(imgContent, modifiedImg);
            }

            // set the Summary to our customized body
            syndicationItem.Summary = new TextSyndicationContent(body, TextSyndicationContentKind.Html);

            //return item
            return syndicationItem;
        }
    }
}

Our custom RSS feed will now display the customized data!



Meet Gorman Law

Full Stack Developer

🏋️🎸🏈

Gorman is a Full Stack Developer and a University of Calgary alumni who has a background in Canada's financial industry. Outside of work, he likes to go rock climbing, try out new ice cream places, and watch sports.

Connect with Gorman