Skip to content

abjerner/Skybrud.Umbraco.RssUtils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Skybrud.Umbraco.RssUtils

Skybrud.Umbraco.RssUtils is a .NET package that easily lets you create RSS feeds on the fly without having to worry about generating the proper XML - the package takes care of that for you.

Since the package is something that I have made for use in Umbraco projects, there is some Umbraco specific code to more easily create new RSS feeds based on IPublishedContent, but the package also plays well without Umbraco, so you can use it with other systems (or just native ASP.NET projects) as well.

Download

Skybrud.Umbraco.RssUtils can be downloaded from NuGet:

http://www.nuget.org/packages/Skybrud.Umbraco.RssUtils/

Example

The package primarily targets Umbraco, so here is a quick example on how the package can be used in a MVC view:

@using System.Xml.Linq
@using Skybrud.Umbraco.RssUtils
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{
    Layout = null;

    // Obviously the news articles shouldn't be hardcoded
    var articles = new[] {
        new {
            Id = 1,
            Name = "This is the first news article",
            Url = "http://example.bjerner.dk/news/this-is-the-first-news-article/",
            PublishDate = new DateTime(2014, 8, 31, 0, 58, 55),
            Teaser = "This is the teaser text of the first news article"
        },
        new {
            Id = 2,
            Name = "This is the second news article",
            Url = "http://example.bjerner.dk/news/this-is-the-second-news-article/",
            PublishDate = new DateTime(2014, 8, 31, 1, 02, 34),
            Teaser = (string) null
        }
    };

    // Initialize a new feed and add the blog posts
    RssFeed feed = new RssFeed {
        Title = "News from Example",
        Link = Model.Content.UrlWithDomain() + "rss",
        Description = "You can now get news from Example as an RSS feed",
        Language = "en-US"
    };
    
    foreach (var article in articles.Take(30)) {
        feed.Add(new RssItem {
            Title = article.Name,
            Link = article.Url,
            PubDate = article.PublishDate,
            Description = article.Teaser,
            Content = article.Teaser,
            Guid = article.Url
        });
    }

    // Write the RSS feed to the response stream
    feed.Write(SaveOptions.None);
    
}

The above example would generate an output like this:

<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/content">
  <channel>
    <title>News from Example</title>
    <link>http://localhost:54722/rss</link>
    <pubDate>Sat, 30 Aug 2014 22:58:55 GMT</pubDate>
    <description>You can now get news from Example as an RSS feed</description>
    <language>en-US</language>
    <item>
      <title>This is the second news article</title>
      <link>http://example.bjerner.dk/news/this-is-the-second-news-article/</link>
      <pubDate>Sat, 30 Aug 2014 23:02:34 GMT</pubDate>
      <guid>http://example.bjerner.dk/news/this-is-the-second-news-article/</guid>
    </item>
    <item>
      <title>This is the first news article</title>
      <link>http://example.bjerner.dk/news/this-is-the-first-news-article/</link>
      <pubDate>Sat, 30 Aug 2014 22:58:55 GMT</pubDate>
      <guid>http://example.bjerner.dk/news/this-is-the-first-news-article/</guid>
      <description><![CDATA[This is the teaser text of the first news article]]></description>
      <content:encoded><![CDATA[This is the teaser text of the first news article]]></content:encoded>
    </item>
  </channel>
</rss>

Changelog

Skybrud.Umbraco.RssUtils 1.0.3

31st of August, 2014

Download

Changelog

  • Changed namespace from Skybrud.Umbraco.Rss to Skybrud.Umbraco.RssUtils to follow package name (See commit)
  • Introduced the description property for RSS items (See commit)
  • Added some extra format options when writing the feed (See commit)
  • Other adjustments throughout the package