In this tutorial, you will learn how to create an RSS feed within Episerver. Like most things in Episerver, if you want to create an RSS feed within your Episerver powered website, you will need to roll up your sleeves and get your hands a bit dirty. Most 'Episerver' problems can be solved using the same tools and frameworks that would you would use to solve a normal .NET problem. This means that when creating a custom RSS feed we can use the SyndicationFeed object. In today's tutorial I'm going to cover the code you'll require to integrate this within Episerver CMS
Creating The RSS Feed
Let us begin by going over the code required to render out an RSS feed. The whole feed will be generated by the SyndicationFeed
class.
In the snippet above I first configure the feeds settings. To create a valid RSS feed you will render out a feed title, description, and a published date. The code at the bottom of the class is all to do with feed validation. Depending on your situation, you may not care about feed validation, however, be aware that some RSS readers won't pick up your feed unless these things are set. Failing to add the correct attributes and elements can cause you headaches. If you are unsure about this, I recommend popping over to the W3C RSS feed validator to check your feed is valid.
Controller Code: In order to render a feed you will need a URL. This means that you will need to create a controller in order to have that end-point your site visitors can call. For this, you can create a normal MVC controller. You will need to register the controller in the sites route table before it will work though! The first thing you'll need to do is return XML back to the client. In a normal controller, you return HTML. To render data as XML we can create a custom ActionResult
:
The code above assumes you have a list of pages already created within Episerver CMS. Getting data out of Episerver is done using the IContentRepostiory
. These page objects will need to be converted to items of type SyndicationItem
. All SyndicationItems
will need to be added within the SyndicationFeed
➡ Feeds
property. The code above should be pretty simple to understand. Gets some objects in a collection, iterate through the collection and convert the objects into a different type. The last line is where the feed will be added into the requests response stream.
RssActionResult: Next, let us create the custom action result to return the data as XML:
This custom ActionResult
will take the response object of type SyndicationFeed
and add it to the response stream:
If everything is set up correctly, when you try and view your feed via a browser, it should look something like the screenshot above. Remember you need to add a route in the routing table for the URL to work! More information on how to do that can be found here. Happy Coding 🤘