Customize RSS Feed

Posted by

RSS ( Rich Site Summary ) uses a family of standard web feed formats to publish frequently updated information: blog entries, news headlines, audio, video. So for setting up this frequently updated RSS Feeds into sitecore we already have a wonderful walkthrough by sitecore.
Please click on the link here to configure RSS Feed into Sitecore.

Moving ahead we got a requirement from the client to customize the above RSS Feeds into Sitecore on basis of the templates.

The requirement is basically to customize the resultant of the RSS Feeds into Sitecore. Rather than having the entire RSS Feeds in there, we want only the relevant and important data to be displayed.

So here we are with the above customization on the top of the setup of RSS Feeds into Sitecore.
So let’s commence.

1. Let’s add 2 custom fields into Extensibility section of RSSFeed Template which can be found at “/sitecore/templates/System/Feeds/RSS Feed”

a. Root Path – DropTree

b. Content Types – Treelist {For Template selection}

RSSFeedTemplate

2. Adding field into content search index.

  
 <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <contentSearch>
      <indexConfigurations>
        <defaultLuceneIndexConfiguration type="Sitecore.ContentSearch.LuceneProvider.LuceneIndexConfiguration, Sitecore.ContentSearch.LuceneProvider">
          <fields hint="raw:AddComputedIndexField">
            <field fieldName="_links">Sitecore.ContentSearch.ComputedFields.ItemLinks, Sitecore.ContentSearch</field>
          </fields>
        </defaultLuceneIndexConfiguration>
      </indexConfigurations>
    </contentSearch>
  </sitecore>
</configuration>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Sitecore.ContentSearch;
using Sitecore.ContentSearch.SearchTypes;
using Sitecore.Data;

namespace [Namespace]
{
    public class Custom : SearchResultItem
    {
        [IndexField("_links")]
        public IEnumerable Links { get; set; }
    }
}

using Sitecore.Configuration;
using Sitecore.ContentSearch;
using Sitecore.ContentSearch.Linq;
using Sitecore.ContentSearch.Linq.Utilities;
using Sitecore.Data;
using Sitecore.Data.Fields;
using Sitecore.Data.Items;
using Sitecore.Syndication;
using SitecoreContentSearchRSSFeeds.Web.SearchTypes;
using System.Collections.Generic;
using System.Linq;

namespace [Namespace]
{
    public class RssFeed : PublicFeed
    {
        private ID rPath = ID.Null;
        private ID RPath
        {
            get
            {
                if (rPath.IsNull)
                    rPath = ((LookupField)this.FeedItem.Fields[Templates.HasFeed.Fields.Rootpath]).TargetID;

                return rPath;
            }
        }

        private IEnumerable _ContentTypes;
        private IEnumerable ContentTypes
        {
            get
            {
                if (_ContentTypes == null)
                    _ContentTypes = ((MultilistField)this.FeedItem.Fields[Templates.HasFeed.Fields.Contenttype]).Items.Select(ID.Parse);

                return _ContentTypes;
            }
        }

        public override IEnumerable GetSourceItems()
        {
            if (ID.IsNullOrEmpty(RPath) || ContentTypes.Any() == false)
                return Enumerable.Empty();

            using (var searcher =ContentSearchManager.GetIndex(string.Format("sitecore_{0}_index", Sitecore.Context.Database.Name)).CreateSearchContext())
            {
                var query = PredicateBuilder.True();

                query = query.And(i => i.Paths.Contains(RPath));

                var contentTypesQuery = PredicateBuilder.False();

                foreach (var itm in ContentTypes)
                {
                    var id = itm;

                    contentTypesQuery = contentTypesQuery.Or(i => i.TemplateId == id);
                }

                query = query.And(contentTypesQuery);

                var maxInFeed = int.Parse(Settings.GetSetting("Feeds.MaximumItemsInFeed", "50"));

                var results = searcher.GetQueryable().Where(query).Take(maxInFeed).GetResults().Hits.Select(i => i.Document.GetItem()).ToList();

                return results;
            }
        }
    }
}

Hope it helps. 🙂

Leave a Reply