RssFeed handler

OpenWaves.Templates.RssFeed package provides a simple aspx handler template for implementing rss channel on your ASP .NET Web Application.
It has dependency on HttpHandler generic type from OpenWaves.EPiServer package.

using System.Collections.Generic;
using System.Linq;
using EPiServer.Core;
using OpenWaves.EPiServer;
using System.ServiceModel.Syndication;
using System.Text;
using System.Xml;
using System.Web;
...
public class RssFeed: HttpHandler<PageData>
{       
    protected virtual string ContentType
    {
        get { return "application/rss+xml"; }
    }

    protected virtual Encoding ContentEncoding
    {
        get { return Encoding.UTF8; }
    }

    public override void ProcessRequest(HttpContext context)
    {
        var feed = this.CreateSyndicationFeed();

        context.Response.ContentType = this.ContentType;
        context.Response.ContentEncoding = this.ContentEncoding;

        using (var xmlTextWriter = this.CreateXmlTextWriter(context))
        {
            feed.SaveAsRss20(xmlTextWriter);
        }                
    }

    protected virtual SyndicationFeed CreateSyndicationFeed()
    {
        var feedRootPage = this.CurrentPage ?? PageReference.StartPage.Resolve();

        var items = feedRootPage.GetChildren()
            .Select(CreateSyndicationItem)
            .Take(10);

        var feed = new SyndicationFeed(items)
            {
                Title = new TextSyndicationContent(feedRootPage.PageName)
            };

        return feed;
    }

    protected virtual SyndicationItem CreateSyndicationItem(PageData pageData)
    {
        return new SyndicationItem
            {
                Id = pageData.PageGuid.ToString(),
                Title = new TextSyndicationContent(pageData.PageName),
                PublishDate = pageData.StartPublish,
            };
    }

    protected virtual XmlTextWriter CreateXmlTextWriter(HttpContext context)
    {
        return new XmlTextWriter(context.Response.OutputStream, this.ContentEncoding);
    }
}
You can modify template or use like a base class for your custom handler.

Usage example

The following example code shows how to use RssFeed template to implement custom rss channel for news.

public class NewsRssFeed : HttpHandler<NewsArchive>
{
    public override void ProcessRequest(HttpContext context)
    {
        var articles = this.CurrentPage.GetChildren().OfType<NewsArticle>();

        var items = articles.Select(CreateSyndicationItem).ToList();
        var feed = new SyndicationFeed(items);
        feed.Title = new TextSyndicationContent(this.CurrentPage.Title);

        context.Response.ContentType = "application/rss+xml";
        context.Response.ContentEncoding = Encoding.UTF8;

        using (var xmlTextWriter = new XmlTextWriter(context.Response.OutputStream, Encoding.UTF8))
        {
            feed.SaveAsRss20(xmlTextWriter);
        }
    }

    private static SyndicationItem CreateSyndicationItem(NewsArticle a)
    {
        var item = new SyndicationItem
        {
            Title = new TextSyndicationContent(a.Title),
            Content = new XhtmlSyndicationContent(a.Body),
            Summary = new TextSyndicationContent(a.Intro),
            PublishDate = a.StartPublish,
            Id = a.PageGuid.ToString()
        };

        var url = a.GetFriendlyUrl().ResolveAgainst(HttpContext.Current.Request.GetRawUrl());
        item.BaseUri = url;
        item.Links.Add(SyndicationLink.CreateAlternateLink(url));

        return item;
    }
}

Last edited Jan 25, 2013 at 1:44 PM by piotrdela, version 6

Comments

No comments yet.