Skip to content

Instantly share code, notes, and snippets.

@Vrijdagonline
Forked from nul800sebastiaan/Rss.cshtml
Created November 9, 2018 13:21
Show Gist options
  • Save Vrijdagonline/3bd275422be77d9b90fe67c2e41a3faa to your computer and use it in GitHub Desktop.
Save Vrijdagonline/3bd275422be77d9b90fe67c2e41a3faa 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment