Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple altTemplate RSS feed - see comments for usage instructions.
@inherits UmbracoTemplatePage
@{
Response.ContentType = "text/xml";
// The variables in this code block are the only ones you might need to change a little to get it to work
var blogName = "Cultiv";
var currentUrl = string.Format("https://{0}", Request.Url.Host);
// Find first node under the root of document type BlogOverview
var blogNode = Model.Content.AncestorOrSelf(1).Descendants("BlogOverview").First();
var allPosts = blogNode.Children().OrderByDescending(c => c.CreateDate);
var latestPostDate = allPosts.Take(1).First().CreateDate.ToString("R");
// Important: the `<?xml` opener needs to be touching the closing `}` so it is valid xml, else the first line is whitespace
}<?xml version="1.0" encoding="UTF-8" ?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
<title>@blogName</title>
<link>@currentUrl</link>
<pubDate>@latestPostDate</pubDate>
<generator>umbraco</generator>
<description></description>
<language>en</language>
<atom:link rel="self" type="application/rss+xml" href="@currentUrl/rss" />
@foreach (var post in allPosts)
{
<item>
<title>@post.Name</title>
<author>info.nospamplease@@@Request.Url.Host (admin)</author>
@Html.Raw("<link>" + currentUrl + post.Url + "</link>")
<pubDate>@post.CreateDate.ToString("R")</pubDate>
<guid>@(currentUrl + post.Url)</guid>
<description>
@Html.Raw("<![CDATA[" + post.GetPropertyValue<string>("bodyText")
.Replace("/media/", currentUrl + "/media/")
.Replace("/blog/", currentUrl + "/blog/") + "]]>")
</description>
<content:encoded>
@Html.Raw("<![CDATA[" + post.GetPropertyValue<string>("bodyText")
.Replace("/media/", currentUrl + "/media/")
.Replace("/blog/", currentUrl + "/blog/") + "]]>")
</content:encoded>
</item>
}
</channel>
</rss>
@nul800sebastiaan

This comment has been minimized.

Copy link
Owner Author

commented Feb 14, 2018

Save as Rss.cshtml in ~/Views and use like: https://site.com/rss or https://site.com/?altTemplate=rss
You need to create a template in the backoffice too, called Rss - make sure it doesn't inherit from any other templates.

For the description and content I'm replacing relative URLs to media items and links to other parts of the blog with the full URL so they will work in an RSS reader.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.