Override Sitecore RSS feed results


As you know Sitecore Rss feed when configured on sitecore items and when browsed to, would show in the result xml, each of those item with title, description mapped to the item fields. But what if you wish to also include other field values in that Rss feed xml. This may be required by external feed clients to search, display results.

 

Here I explain a simple mechanism to achieve this.

 

1- You need to create an item out of Template "/sitecore/templates/System/Feeds/RSS Feed" ({B960CBE4-381F-4A2B-9F44-A43C7A991A0B}).

 

2- You can fill in the required field values for "Items", "Title" and "Description" as needed.

 

3- So now when you browse to view the RSS feed output you will notice the standard RSS item nodes and values.

 

4- Since you wish to also include other sitecore item fields (fields of child items of "Items" field specified in step 2) in this RSS feed, you can overwrite the standard RSS layout with your own layout as shown below:

 

using Sitecore;

using Sitecore.Data.Items;

using Sitecore.Diagnostics;

using Sitecore.Exceptions;

using Sitecore.Security.Accounts;

using Sitecore.Shell.Feeds;

using Sitecore.Sites;

using Sitecore.Syndication;

 

using System.ServiceModel.Syndication;

 

   public partial class YourFeedDeliveryLayout : global::Sitecore.Syndication.Web.FeedDeliveryLayout

    {

        protected override void OnLoad(EventArgs e)

        {

            base.OnLoad(e);

        }

 

        protected override void OnInit(EventArgs e)

        {

            FeedUrlOptions feedUrlOption;

            Assert.ArgumentNotNull(e, "e");

            Sitecore.Context.Site.SetDisplayMode(DisplayMode.Normal, DisplayModeDuration.Temporary);

            Item item = Sitecore.Context.Item;

            Assert.IsNotNull(item, "current item");

            base.Response.Clear();

            base.Response.ContentType = "text/xml";

            try

            {

                feedUrlOption = FeedUrlOptions.ParseQueryString();

            }

            catch (SyndicationUrlHashMismatchException syndicationUrlHashMismatchException)

            {

                Log.Error("Sitecore RSS failed to authenticate user requesting the RSS feed", syndicationUrlHashMismatchException, this);

                throw;

            }

            UserSwitcher userSwitcher = null;

            if (feedUrlOption.UseUrlAuthentication)

            {

                userSwitcher = new UserSwitcher(feedUrlOption.Username, true);

            }

            using (userSwitcher)

            {

                SyndicationFeed syndicationFeed = FeedManager.GetFeed(item).Render();

                string str = FeedManager.Render(syndicationFeed);

                base.Response.Output.Write(ModiyRss(str));

            }

            base.Response.End();

        }

 

        private string ModiyRss(string html)

        {

            XmlDocument doc = new XmlDocument();

            doc.LoadXml(html);

 

            XmlNodeList items = doc.SelectNodes("//item");

 

            foreach (XmlNode item in items)

            {

                XmlNode idNode = item.ChildNodes[0];

                string id = idNode.InnerText;

 

                Sitecore.Data.Items.Item contextItem = Sitecore.Context.Database.Items[new Sitecore.Data.ID(id)];

                if (contextItem != null)

                {

                    XmlElement customItemField = doc.CreateElement("[your custom field name as needed in RSS feed]");

                    customItemField.InnerText = contextItem["your custom sitecore item field"];

                    item.AppendChild(customItemField);

                   

                    //...you can add more such appends of custom fields as needed

                }

 

            }

 

            return doc.OuterXml;

 

        }

 

    }

   

5- Create the sitecore Layout item for this new layout file you just created.

 

6- You can now change the presentation settings of the RSS feed item you created at step 1 and set the layout as this new one (step 5) you just created.

 

7- Now when you browse to this RSS feed item you will also see your new custom fields.

Categories: Sitecore, Sitecore Rss
Tags: Sitecore, Rss;

SEARCH ARTICLES