Anders G. Nordby

Senior Web Developer at CGI

RssReaderBlock in MVC

The customer found this RssReaderBlock, and wanted me to include it in the project. Upon closer inspection of the content, I found that it would be better to rewrite this into MVC format. In case anyone else needs this, here is the MVC files:

using System;
using System.ComponentModel.DataAnnotations;
using EPiServer;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.DataAnnotations;

//http://world.episerver.com/blogs/Jeff-Wallace/Dates/2013/5/RSS-Reader-Block/
namespace MyProject.Delegate.Models.Blocks
{
    [ContentType(DisplayName = "RSS Reader Block", GUID = "8fc5a3bb-727c-4871-8b2e-5ff337e30e82", Description = "Display content from a RSS feed", GroupName = "Syndication")]
    [ImageUrl("~/Static/gfx/block-type-thumbnail-rss.png")]
    public class RssReaderBlock : BlockData
    {
        [CultureSpecific]
        [Editable(true)]
        [Display(
            Name = "RSS Feed URL",
            Description = "URL for RSS feed",
            GroupName = SystemTabNames.Content,
            Order = 1)]
        [Required]
        public virtual Url RssUrl { get; set; }

        [Editable(true)]
        [Display(
            Name = "Heading",
            Description = "Heading for the RSS feed",
            GroupName = SystemTabNames.Content,
            Order = 2)]
        [CultureSpecific]
        public virtual String Heading { get; set; }

        [Editable(true)]
        [Display(
            Name = "Descriptive Text",
            Description = "Descriptive text for the RSS feed",
            GroupName = SystemTabNames.Content,
            Order = 3)]
        [CultureSpecific]
        public virtual XhtmlString DescriptiveText { get; set; }

        [Editable(true)]
        [Display(
            Name = "Max Count",
            Description = "Maximum number of items to display",
            GroupName = SystemTabNames.Content,
            Order = 4)]
        public virtual int MaxCount { get; set; }

        [Editable(true)]
        [Display(
            Name = "Include Publish Date",
            Description = "Include publish date for each item in list",
            GroupName = SystemTabNames.Content,
            Order = 5)]
        public virtual bool IncludePublishDate { get; set; }

        /// <summary>
        /// Sets the default property values on the content data.
        /// </summary>
        /// <param name="contentType">Type of the content.</param>
        public override void SetDefaultValues(ContentType contentType)
        {
            base.SetDefaultValues(contentType);

            MaxCount = 5;
            IncludePublishDate = false;
        }
    }
}
using System.Collections.Generic;
using MyPRoject.Delegate.Models.Blocks;
using EPiServer.Core;

namespace MyProject.Delegate.Models.ViewModels
{
    public class RssReaderBlockViewModel
    {
        public RssReaderBlock CurrentBlock { get; set; }

        public XhtmlString DescriptiveText { get; set; }
        public bool HasHeadingText { get; set; }
        public string Heading { get; set; }
        public List<RssItem> RssList { get; set; }

        public class RssItem
        {
            public string Title { get; set; }
            public string Url { get; set; }
            public string PublishDate { get; set; }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Xml.Linq;
using MyProject.Delegate.Models.Blocks;
using MyProject.Delegate.Models.ViewModels;
using EPiServer.Web.Mvc;

//http://world.episerver.com/blogs/Jeff-Wallace/Dates/2013/5/RSS-Reader-Block/
namespace MyProject.Tekna.Controllers
{
    public class RssReaderBlockController : BlockController<RssReaderBlock>
    {
        public override ActionResult Index(RssReaderBlock currentBlock)
        {
            var viewModel = new RssReaderBlockViewModel
            {
                RssList = new List<RssReaderBlockViewModel.RssItem>(),
                CurrentBlock = currentBlock
            };

            try
            {
                if ((currentBlock.RssUrl != null) && (!currentBlock.RssUrl.IsEmpty()))
                {
                    var rssDocument = XDocument.Load(Convert.ToString(currentBlock.RssUrl));

                    var posts = from item in rssDocument.Descendants("item").Take(currentBlock.MaxCount)
                                select new RssReaderBlockViewModel.RssItem
                                {
                                    Title = item.Element("title").Value,
                                    Url = item.Element("link").Value,
                                    PublishDate = item.Element("pubDate").Value,
                                };

                    viewModel.RssList = posts.ToList();
                    viewModel.HasHeadingText = HasHeadingText(currentBlock);
                    viewModel.Heading= currentBlock.Heading;
                    viewModel.DescriptiveText = currentBlock.DescriptiveText;
                }
            }
            catch (System.Net.WebException)
            {

            }
            catch (System.IO.FileNotFoundException)
            {

            }


            return View("~/Views/RssReaderBlock/Index.cshtml", viewModel);
        }

        private bool HasHeadingText(RssReaderBlock currentBlock)
        {
            return ((!string.IsNullOrEmpty(currentBlock.Heading)) || ((currentBlock.DescriptiveText != null) && (!currentBlock.DescriptiveText.IsEmpty)));
        }
    }
}
@model RssReaderBlockViewModel
@{
    Layout = "";
}

<div class="rssfeed">
    @if (Model.HasHeadingText)
    {
        <h2>@Model.Heading</h2>
        <p>@Model.DescriptiveText</p>
    }
    <ul>
        @foreach (var item in Model.RssList)
        {
            <li>
                <div class="listResult">
                    <h3>
                        <a href="@item.Url" title="@item.Title">@item.Title</a>
                    </h3>
                    @if (Model.CurrentBlock.IncludePublishDate)
                    {
                        <p class="date">@item.PublishDate</p>
                    }
                    <hr />
                </div>
            </li>
        }
    </ul>
</div>
Follow

Get every new post delivered to your Inbox.