Last active
November 9, 2018 13:21
-
-
Save nul800sebastiaan/d112734c31c0afeb6ae0e4fe2b129c4f to your computer and use it in GitHub Desktop.
Simple altTemplate RSS feed - see comments for usage instructions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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
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
andcontent
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.