Friday, December 15, 2006

Extending the RSS Module

the RSS module i really like. it's a good example of a great module that does what it's supposed to be doing. Alexey Rusakov is a wizard, can't put it any other way. his last posts about deletion workflow are really interesting and if you've ever thought about custom workflows i encourage you to read (apart from all the stuff at SDN5) Alexey's Deletion Workflow Part 1 & 2, and also Alex Shyba's Workflow with unpublishing article. now, with that said, i just thought i'd share a little example of how you can easily make your pages 'advertise' that there's feed content available on the page. modern browsers 'light up' to indicate a feed is present, and it's really, really, really simple - it's just a link in the header to the atual rss istelf. you don't need to have the RSS module, you can of course create your own, but i strongly recommend that you use the module. so, assuming you have the module installed & running you'd have a node somewhere that stores your feeds, here's what you do: 1) add a new Tree list field (i -so- love this field, i use it all the time) called "RSS_Feeds" to your content templates (even better if you got a sitewide template your content templates inherit from) and set the source of that field to your Feeds node (like '/sitecore/content/global/rss feeds' or something). 2) create a new class called RssAlternativeRenderer - this class will output the alternative links to the header of your page 3) add the following code to your new class
public static void GetRssUrls() { Item root = Sitecore.Context.Item; System.Text.StringBuilder sb = new System.Text.StringBuilder(); if (root != null) { if (root.Fields["Rss_Feeds"]!=null) { Sitecore.Data.Fields.MultilistField mf = root.Fields["Rss_Feeds"]; if (mf != null) { if (mf.Count > 0) { foreach (Item itm in mf.GetItems()) { sb.AppendLine("<link rel=\"alternate\" type=\"application/rss+xml\" title=\"" + itm.Fields["Title"].Value.ToString() + "\" href=\"" + itm.Paths.GetFriendlyUrl() + "\" />"); } } } Sitecore.Context.Page.Page.Header.Controls.Add(new LiteralControl(sb.ToString())); } } }
4) add the following code to your Page_Load event

protected void Page_Load(object sender, EventArgs e) { RssAlternativeRenderer.GetRssUrls(); }

that's it. now compatible browsers will 'light up' all shiny and stuff when you select a feed that will be available on a page - ain't that great for christmas? finally, wrapping up, Alex de Groot just posted his 1-year-blogging-anniversary post. Congratulations! today is friday - today is afterwork - today is gonna be one more day that i forget to go buy gifts.. but hey, at least it's friday.. take care, P.

1 comment:

Anonymous said...

Hi Peter,

First of all, thanks for the thumbs up.

Good point about adding rss feeds to the pages, I think we should include that in the RSS module.

In the end, it would be fair play, since both demosite and www.sitecore.net do have the rss linked the right way :)