How to create an RSS & Atom feed in Umbraco V9
Whilst building the new UmbHost website in Umbraco V9, I decided I would move any Umbraco blog posts, or anything hosting / server related to the blog over there.
Of course this meant I needed to add an RSS / Atom feed to the site, on the Umbraco Discord server I came across a really useful tip, we can use Route
in a surface controller, this meant I could use blog/feed.rss
and blog/feed.atom
easily.
Below you will find the code I have used to do this, I'm sure there are improvements which can be made (please do let me know in the comments), I have also set the feeds to be cached every 15 minutes.
The dot net package called System.ServiceModel.Syndication
will need to be installed into your project using the following command
dotnet add package System.ServiceModel.Syndication
I have also added a checkbox property to my blog post document type, with an alias of hideFromXmlSyndication
this is used to hide the blog item from the feeds, this defaults to false
First we want to create an interface called ISyndicationXmlService.cs
Next create the service called SyndicationXmlService.cs
Now we want to register the service using a composer called RegisterSyndicationXmlServiceComposer.cs
Finally we need a surface controller to return the feeds called SyndicationSurfaceController.cs
In here you can change the Route
to your preferred url and changing the ResponseCache
will change the length of time the feed is cached for (in seconds)