Skip to content

Instantly share code, notes, and snippets.

@sleslie321
Created August 23, 2014 14:12
Show Gist options
  • Save sleslie321/5d2e11520f45b5611f1d to your computer and use it in GitHub Desktop.
Save sleslie321/5d2e11520f45b5611f1d to your computer and use it in GitHub Desktop.
Create RSS Feed from Document Type in Umbraco using Razor
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@{umbraco.library.ChangeContentType("text/xml");
var siteURL = "http://" + Request.Url.Host;
var HomePage = CurrentPage.AncestorOrSelf(1);
var RSSFeedPubDate = HomePage.Descendants("PressRelease").OrderBy("publishedDate desc").First();
}<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rssdatehelper="urn:rssdatehelper">
<channel>
<title>Press Releases</title>
<link>@siteURL</link>
<pubDate>@RSSFeedPubDate.UpdateDate.ToString("r")</pubDate>
<generator>Umbraco</generator>
<description>Latest Press Releases</description>
<language>en</language>
@{
foreach (var release in HomePage.Descendants("PressRelease").OrderBy("publishedDate desc").Take(10))
{
<item>
<title>@release.Name</title>
@Html.Raw("<link>")@[email protected](release.Url)@Html.Raw("</link>")
<description>@release.Summary</description>
<pubDate>@release.publishedDate.ToString("r")</pubDate>
<guid>@[email protected](release.Url)</guid>
</item>
}
}
</channel>
</rss>
@walter-the-coder
Copy link

Hi! I saw your instruction on how to make setup a RSS feed on http://sleslie.me/2014/rss-feed-with-razor-in-umbraco/

Everything worked like a charm, thumbs up, but I noticed a litte bug your instructions where you say
Create a partial view macro called 'Razor', create Macro at same time.
I believe it should say RSS here, not 'Razor'

@bjarnef
Copy link

bjarnef commented Jan 29, 2016

Why not write this line more simple? :)

@Html.Raw("<link>")@[email protected](release.Url)@Html.Raw("</link>")

so instead this:

<link>@(siteURL + release.Url)</link>

or

<link>@(String.Format("{0}{1}", siteURL, release.Url))</link>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment