Just Code‎ > ‎

EPiServer RSS Page provider

posted Jan 19, 2010, 12:41 PM by Peter Henell   [ updated Jan 20, 2010, 3:58 AM ]
First create the buissiness object needed to hold the RSS feed and items
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;

namespace EPiServer.Labs.PageProviders
{ public class RSSFeed
{ public Guid GUID { get; set; } public string Name { get; set; } public string Description { get; set; } private int _ID; public int ID
{ get { return BitConverter.ToInt32(GUID.ToByteArray(), 0); } } private List<RSSFeedItem> _Items; public RSSFeedItem[] Items
{ get { return _Items.ToArray(); } } public void Load(string url) { XDocument doc = XDocument.Load(url); XElement channelElement = doc.Root.Element(XName.Get("channel")); Name = channelElement.Element(XName.Get("title")).Value; Description = channelElement.Element(XName.Get("description")).Value; byte[] urlBytes = System.Text.Encoding.ASCII.GetBytes(url); byte[] guidBytes = new byte[16]; for (int i = 0; i < guidBytes.Length; i++) { if (urlBytes.Length > i) guidBytes[i] = urlBytes[i]; else guidBytes[i] = 0; } GUID = new Guid(guidBytes); _Items = new List<RSSFeedItem>(); foreach (var item in doc.Root.Descendants(XName.Get("item"))) { _Items.Add(new RSSFeedItem(item)); } } } public class RSSFeedItem
{ public Guid GUID { get; set; } public string Title { get; set; } public string Description { get; set; } public String Link { get; set; } public DateTime PublicationDate { get; set; } public int ID
{ get { return BitConverter.ToInt32(GUID.ToByteArray(), 0); } } public RSSFeedItem(XElement itemElement) { GUID = GetGUID(itemElement, "guid"); Title = GetElementValue(itemElement, "title"); Description = GetElementValue(itemElement, "description"); Link = GetElementValue(itemElement, "link"); PublicationDate = DateTime.Parse(GetElementValue(itemElement, "pubDate")); } private string GetElementValue(XElement itemElement, string elementName) { return itemElement.Element(XName.Get(elementName)).Value; } private Guid GetGUID(XElement itemElement, string elementName) { string guid = GetElementValue(itemElement, elementName); //guid = guid.Substring(guid.Length - 36); //return new Guid(guid); byte[] urlBytes = System.Text.Encoding.ASCII.GetBytes(StringReverse(guid)); byte[] guidBytes = new byte[16]; for (int i = 0; i < guidBytes.Length; i++) { if (urlBytes.Length > i) guidBytes[i] = urlBytes[i]; else guidBytes[i] = 0; } return new Guid(guidBytes); } private string StringReverse(string input) { char[] strArray = input.ToCharArray(); Array.Reverse(strArray); string strReversed = new string(strArray); return strReversed; } } }



Then implement PageProviderBase
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using System.Text.RegularExpressions;

namespace EPiServer.Labs.PageProviders
{ public class RSSPageProvider : PageProviderBase
{ public RSSFeed Feed { get; private set; } public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config) { base.Initialize(name, config); string url = config["rssUrl"]; Feed = new RSSFeed(); Feed.Load(url); } private bool IsFeedPage(PageReference pageLink) { return pageLink.ID == Feed.ID; } private bool IsFeedPage(Guid pageGuid) { return pageGuid == Feed.GUID; } private RSSFeedItem GetFeedItem(PageReference pageLink) { return Feed.Items.SingleOrDefault(itm => itm.ID == pageLink.ID); } private RSSFeedItem GetFeedItem(Guid pageGuid) { return Feed.Items.SingleOrDefault(itm => itm.GUID == pageGuid); } private Uri GetFeedUri() { return ConstructPageUri(PageType.Load("RSSFeed").ID, ConstructPageReference(Feed.ID)); } private Uri GetFeedItemUri(RSSFeedItem item) { return ConstructPageUri(PageType.Load("RSSFeedItem").ID, ConstructPageReference(item.ID)); } private void InitializeFeedPage(PageData pd) { InitializePageData( pd, Feed.Name, "RSSFeed", Feed.GUID, ConstructPageReference(Feed.ID), EntryPoint, new List<string>() { "en" }); pd.SetValue("Description", Feed.Description); } private void InitializeFeedItemPage(PageData pd, RSSFeedItem item) { InitializePageData(pd, item.Title, "RSSFeedItem", item.GUID, ConstructPageReference(item.ID), ConstructPageReference(Feed.ID), new List<string>() { "en" }); pd.SetValue("Description", item.Description); pd.SetValue("Link", item.Link); pd.SetValue("PublicationDate", item.PublicationDate); pd.SetValue("PageVisibleInMenu", false); } protected override PageReferenceCollection GetChildrenReferences(PageReference pageLink, string languageID) { PageReferenceCollection pages = new PageReferenceCollection(); if (pageLink == this.EntryPoint) { pages.Add(base.ConstructPageReference(Feed.ID)); } else if (IsFeedPage(pageLink)) { foreach (var item in Feed.Items) { pages.Add(base.ConstructPageReference(item.ID)); } } return pages; } protected override PageData GetLocalPage(PageReference pageLink, ILanguageSelector languageSelector) { PageData pd = new PageData(); if (IsFeedPage(pageLink)) { InitializeFeedPage(pd); } else { RSSFeedItem item = GetFeedItem(pageLink); if (item != null) { InitializeFeedItemPage(pd, item); } } string seg = Regex.Replace(pd.PageName.Replace(' ', '-'), @"^A-Za-z0-9\-\~{1}", ""); pd.SetValue("PageURLSegment", seg); SetPageStatus(pd, VersionStatus.Published); pd.StartPublish = DateTime.Now.AddMinutes(-1); return pd; } protected override Uri ResolveLocalPage(PageReference pageLink, out Guid guid) { guid = Guid.Empty; if (pageLink.RemoteSite != this.Name) { return null; } if (IsFeedPage(pageLink)) { guid = Feed.GUID; return ConstructPageUri(PageType.Load("RSSFeed").ID, ConstructPageReference(Feed.ID)); } else { RSSFeedItem item = GetFeedItem(pageLink); guid = item.GUID; return ConstructPageUri(PageType.Load("RSSFeedItem").ID, ConstructPageReference(item.ID)); } } protected override Uri ResolveLocalPage(Guid pageGuid, out PageReference pageLink) { if (IsFeedPage(pageGuid)) { pageLink = ConstructPageReference(Feed.ID); return GetFeedUri(); } else { RSSFeedItem item = GetFeedItem(pageGuid); if (item != null) { pageLink = ConstructPageReference(item.ID); return GetFeedItemUri(item); } } pageLink = PageReference.EmptyReference; return null; } } }




Lastly, in the <episerver>-tag in web.config add the pageprovider.
Entrypoint is the node in the EPiServer tree where our pageprovider will handle the pages.
rssURL is the URL of the rss feed we want to show.

<pageProvider>
<providers>
<add name="RSSPageProvider" type="EPiServer.Labs.PageProviders.RSSPageProvider, EPiServer.Templates.Public"
entryPoint="32"
rssUrl="http://www.dn.se/m/rss/toppnyheter"/>
</providers>

</pageProvider>
</episerver>





Now we can create an aspx-page that displays our RSS feed.
<%@ Page language="c#" Inherits="EPiServer.Labs.PageProviders.rssfeed" Codebehind="rssfeed.aspx.cs" MasterPageFile="~/Templates/Public/MasterPages/MasterPage.master" %>


<asp:Content runat="server" ContentPlaceHolderID="MainBodyRegion"> <EPiServer:PageList runat="server" PageLinkProperty="PageLink"> <ItemTemplate> <strong><EPiServer:Property Runat="server" PropertyName="PageName" /></strong><br /> <p><%# Container.CurrentPage["Description"] %></p> </ItemTemplate> </EPiServer:PageList> </asp:Content>
ċ
PageProviders.zip
(3k)
Peter Henell,
Jan 20, 2010, 3:57 AM
ċ
Peter Henell,
Jan 20, 2010, 3:57 AM
Comments