Skip to content

Instantly share code, notes, and snippets.

@nul800sebastiaan
Last active November 9, 2018 13:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nul800sebastiaan/d112734c31c0afeb6ae0e4fe2b129c4f to your computer and use it in GitHub Desktop.
Save nul800sebastiaan/d112734c31c0afeb6ae0e4fe2b129c4f to your computer and use it in GitHub Desktop.
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
Copy link
Author

nul800sebastiaan 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