Posts Tagged ‘CSHTML’

I’m using the first Umbraco 5 (Jupiter) RTM on a new project.

One of the requirements of this project is to aggregate RSS feeds, so I built a simple plugin leveraging the System.Xml.Linq objects.

To make the plugin, I created a new MVC 3 project in visual studio to build and test the functionality.
I would have added the Umbraco test site to my solution and setup post build commands to copy the files from my project to Umbraco folders, but for some reason I was not able to run the Umbraco site from Visual Studio. I think its a system problem though.

I’ve also used the HTML Agility pack to parse the feed descriptions and remove bad markup.

Here is the  code to grab the RSS Feed and parse it with the HTMLAgilityPack –

    public static class Reader
         public static IEnumerable GetRssFeed(string feedUrl, bool trimDescription = true)
             XDocument feedXml = XDocument.Load(feedUrl);
             XNamespace dc = "";
             string exp = @"^.{1,500}";
             if (trimDescription == false)
                 exp = ".*";
             var feeds = from feed in feedXml.Descendants("item")
                         select new Rss
                             Title = feed.Element("title").Value,
                             Link = feed.Element("link").Value,
                             Description = Regex.Match(ParsedDoc(feed.Element("description").Value).DocumentNode.InnerText, exp).Value,
                             PubDate = feed.Element("pubDate").Value,
                             Creator = feed.Element(dc + "creator").Value
             return feeds;

         public static HtmlAgilityPack.HtmlDocument ParsedDoc(string source)
             HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
             doc.OptionWriteEmptyNodes = true;
             doc.OptionFixNestedTags = true;
             return doc;



And the RSS class…


    public class Rss : IComparable
        public string Link { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public DateTime PubDateDate { get; set; }
        public string Creator { get; set; }

        public string PubDate 
                return _pubDate;
                _pubDate = value;
                DateTime pubDateDate = DateTime.MinValue;
                DateTime.TryParse(value, out pubDateDate);
                PubDateDate = pubDateDate;
        private string _pubDate;

        // Implement the default CompareTo method with the PubDate
        // as the parameter. 
        public int CompareTo(Rss other)
            // If other is not a valid object reference, this instance is greater.
            if (other == null) return 1;
            return PubDateDate.CompareTo(other.PubDateDate);


In Umbraco, I created a folder in the plugins directory following the Dev Dataset examples.
I placed the HTMLAgilityPack DLL and XML, and my DLL in this folder to allow the CSHTML to call the classes.

I created 2 new document types in Umbraco for an ‘RSS List’ and ‘RSS Feed Item’. On the RSS List template, I grab all the children of that node (which are of type ‘RSS Feed Item’) and grab the properties from them using the DynamicModel.

In the Umbraco RSS List template…

@inherits Umbraco.Cms.Web.RenderViewPage

@functions {
    //Add public properties here to create Macro Parameters

    protected List<Zeroth.Umbraco.RSSReader.Rss> aggregatedFeed = new List<Zeroth.Umbraco.RSSReader.Rss>();

    foreach (var child in DynamicModel.Children)
        aggregatedFeed.AddRange(Zeroth.Umbraco.RSSReader.Reader.GetRssFeed(child.url, true));


<div class="rssList">
@foreach (var child in aggregatedFeed)
    <div class="rssItem">
        <div class="rssLink">
            <a href="@child.Link" target="_blank">@Html.Encode(child.Title)</a>
        <div class="rssByLine">
            <span class="rssPubDate">@child.PubDate.Replace("+0000", "")</span><span> | </span><span class="rssCreator">@child.Creator</span>
        <div class="rssDescription">
            @( new HtmlString(child.Description) )