How to Create an RSS Feed in EPiServer (ASP.NET MVC)

Thanks to Ted and a guy from CodeInside for your blogs which helped come up with this solution.

Problem: Create an RSS feed for ASP.NET MVC Website with EPiServer

Solution: We need three things

1. RssResult class
2. RssPage page type
3. RssPageController which creates the xml rss feed

So let's begin with the RssResult class. This class needs to inherit FileResult (System.Web.Mvc) passing rss+xml as content type.

 public class RssResult : FileResult  
 {  
      private readonly SyndicationFeed _feed;  
      public RssResult(SyndicationFeed feed)  
           : base("application/rss+xml")  
      {  
           _feed = feed;  
      }  
      public RssResult(string title, List<SyndicationItem> feedItems)  
           : base("application/rss+xml")  
      {  
           _feed = new SyndicationFeed(title, title, HttpContext.Current.Request.Url) { Items = feedItems };  
      }  
      protected override void WriteFile(HttpResponseBase response)  
      {  
           using (XmlWriter writer = XmlWriter.Create(response.OutputStream))  
           {  
                _feed.GetRss20Formatter().WriteTo(writer);  
           }  
      }  
 }   

Then, we create a model page type which I've named RssPage.cs. It basically only has one property which holds the root page of the RSS

 [ContentType(  
      DisplayName = "Rss Page",   
      GUID = "2ba86356-847f-439f-bf1c-36f7cf70158e",   
      Description = "Used to create an RSS Feed")]  
 public class RssPage : SitePageData  
 {  
      [Display(  
           Name = "RSS Root Page",  
           GroupName = SystemTabNames.Content,  
           Order = 10)]  
      [Required]  
      public virtual PageReference RssPageLink { get; set; }  
      public PageDataCollection GetPages()  
      {  
           var pages = DataFactory.Instance.GetChildren(RssPageLink);  
           return FilterForVisitor.Filter(pages);  
      }  
 }  

Now we create the controller for this page type.

 public class RssPageController : PageControllerBase<RssPage>  
 {  
      public ActionResult Index(RssPage currentPage)  
      {  
           return Feed(currentPage);  
      }  
      public virtual ActionResult Feed(RssPage currentPage)  
      {  
           var items = new List<SyndicationItem>();  
           var newsPages = DataFactory.Instance.GetChildren<NewsPage>(currentPage.RssPageLink);  
           var pageUri = currentPage.RssPageLink.GetUri();  
           foreach(var newsPage in newsPages)  
           {  
                var feedPackageItem = new SyndicationItem(newsPage.PageTitle, newsPage.Description, pageUri);  
                feedPackageItem.PublishDate = newsPage.StartPublish;  
                items.Add(feedPackageItem);  
           }  
           return new RssResult("My News Feed", items);  
      }  
 }  

Now we create an instance of this page on EPiServer.

Navigating to this page now shows an xml rss page


Any questions, please comment!

Labels: , , ,