Web developers wiki ASP.NET Sitecore Sharepoint Kentico by Evident Interactive

The newer Sitecore (6.1+) versions are able to automatically generate RSS feeds. If you want to create your custom feed or are using a older version of Sitecore you can use this code as a guideline. This is an .ASHX handler to generate a valid RSS feed. It takes a itemLanguage parameter to get the right content language.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.SessionState;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Web;
using Project.Namespace.Properties;
using Sitecore.Shell.Applications.WebEdit.Commands;
using Sitecore.Collections;
using Sitecore;
using System.Xml;
using System.Text;

namespace Project.Namespace.rss
{
	public class Newslisting : IHttpHandler
    {
		
		public string itemID = string.Empty;
        public string itemLanguage = string.Empty;

        public int Total = 0;

		public bool IsReusable
		{
			get
			{
				return false;
			}
		}

		public void ProcessRequest(HttpContext context)
                {
			if (context.Request.QueryString.ToString() != string.Empty)
                         {
				itemID = context.Request.QueryString"newsid".ToString();
				itemLanguage = context.Request.QueryString"language".ToString();
				context.Response.ContentType = "application/rss+xml";
                                Item newsListing = Sitecore.Context.Database.GetItem(itemID);
                                CreateRSS(newsListing.GetFieldValue("Newslisting_Source", itemID));
                         }
                else
                {
                return;
                }
              }

        /// <summary>
        /// Creates the RSS.
        /// </summary>
        /// <param name="itemID">The item ID.</param>
        private void CreateRSS(string itemID)
        {
            #if !debug
            try
            {
            #endif
				
                if (itemLanguage != null)
                {
                    Sitecore.Globalization.Language scLanguage = Sitecore.Globalization.Language.Parse(itemLanguage);
                    Sitecore.Context.Language = scLanguage;
                }
                ID ItemID = new ID(itemID);
                
                Item currentNewslisting = Sitecore.Context.Database.GetItem(ItemID);
                int count = 0;

				XmlTextWriter xtw = new XmlTextWriter(HttpContext.Current.Response.OutputStream, Encoding.UTF8);
				xtw.WriteStartDocument();
				xtw.WriteStartElement("rss");
				xtw.WriteAttributeString("version", "2.0");
				xtw.WriteStartElement("channel");
				xtw.WriteElementString("title", currentNewslisting.Fields"Newslisting_Title".Value);
				xtw.WriteElementString("pubDate", currentNewslisting.Statistics.Created.ToString("r"));
				xtw.WriteElementString("lastBuildDate", DateTime.Now.ToString("r"));
				xtw.WriteElementString("description", StringUtil.RemoveTags(currentNewslisting.Fields"Newslisting_Introduction".Value));				xtw.WriteElementString("link", "http://" + WebUtil.GetHostName() + HttpUtility.UrlPathEncode(currentNewslisting.GetFriendlyUrl()));

                foreach (Item currentitem in currentNewslisting.Children)
                {
                    if(currentitem.TemplateID.ToString() == Settings.Default.TemplateNewsdetailID)
                    {
                        if (count < 10)
                        {
                            Sitecore.Globalization.Language language = Sitecore.Globalization.Language.Parse(itemLanguage);
                            int verCount = Sitecore.Data.Managers.ItemManager.GetVersions(currentitem, language).Count;

                            if (verCount > 0)
                            {
								xtw.WriteStartElement("item");
								xtw.WriteElementString("title", currentitem.Fields"Newsdetail_Title".Value);
								xtw.WriteElementString("description", StringUtil.RemoveTags(currentitem.Fields"Newsdetail_DescriptionRSS".Value));
								xtw.WriteElementString("link", "http://" + WebUtil.GetHostName() + HttpUtility.UrlPathEncode(currentitem.GetFriendlyUrl()));
								xtw.WriteElementString("guid", "http://" + WebUtil.GetHostName() + HttpUtility.UrlPathEncode(currentitem.GetFriendlyUrl()));
								xtw.WriteElementString("pubDate", currentitem.Statistics.Created.ToString("r"));
								xtw.WriteEndElement();
                            }
                        }
                    }
        
                }

				xtw.WriteEndElement();
				xtw.WriteEndElement();
				xtw.WriteEndDocument();

				xtw.Flush();
				xtw.Close();


            #if !debug
            }
            catch(Exception ex)
            {

                Sitecore.Diagnostics.Log.Error("RSS could not be created for " + itemID + ex, this);
                
            }
            #endif
        }
    }
}



 © Evident Interactive BV