Sitecore upgrade from 6.5 to 7 – scSearchContrib DynamicFields

In our project we had half a dozen dynamic fields that needed to be migrated from the scSearchContrib way of defining dynamic fileds to the Sitecore 7 way.

This post from John West paved the path for conversion: http://www.sitecore.net/Learn/Blogs/Technical-Blogs/John-West-Sitecore-Blog/Posts/2013/03/Sitecore-7-Computed-Index-Fields.aspx

Note the highlighted common code between the before and after code snapshots below:

The code went from:

using Sitecore.Data.Items;
using scSearchContrib.Crawler.DynamicFields;

namespace Web.Modules.Search.DynamicFields
{
    public class IsApplicableRouteEmptyDynamicField : BaseDynamicField
    {
        public override string ResolveValue(Item item)
        {
            var applicableRouteField = item.Fields["Applicable Routes"];
            if (applicableRouteField != null && !string.IsNullOrWhiteSpace(applicableRouteField.Value))
            {
                return "0";
            }

            return "1";
        }
    }
}

To:

using System;
using Sitecore;
using Sitecore.ContentSearch;
using Sitecore.ContentSearch.ComputedFields;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;

namespace Web.Modules.Search.DynamicFields
{
    // Implementation taken from: http://www.sitecore.net/Learn/Blogs/Technical-Blogs/John-West-Sitecore-Blog/Posts/2013/03/Sitecore-7-Computed-Index-Fields.aspx
    public class IsApplicableRouteEmptyDynamicField : IComputedIndexField
    {
        public string FieldName { get; set; }
        public string ReturnType { get; set; }

        public object ComputeFieldValue(IIndexable indexable)
        {
            Assert.ArgumentNotNull(indexable, "indexable");
            SitecoreIndexableItem scIndexable = indexable as SitecoreIndexableItem;

            if (scIndexable == null)
            {
                Log.Warn("unsupported IIndexable type : " + indexable.GetType(), this);
                return false;
            }

            Item item = (Item)scIndexable;

            if (item == null)
            {
                Log.Warn("unsupported SitecoreIndexableItem type : " + scIndexable.GetType(), this);
                return false;
            }

            // optimization to reduce indexing time
            // by skipping this logic for items in the Core database
            if (String.Compare(item.Database.Name, "core", StringComparison.OrdinalIgnoreCase) == 0)
            {
                return false;
            }

            if (item.Paths.IsMediaItem)
            {
                return item.TemplateID != TemplateIDs.MediaFolder
                  && item.ID != ItemIDs.MediaLibraryRoot;
            }

            if (!item.Paths.IsContentItem)
            {
                return false;
            }

            var applicableRouteField = item.Fields["Applicable Routes"];
            if (applicableRouteField != null && !string.IsNullOrWhiteSpace(applicableRouteField.Value))
            {
                return "0";
            }

            return "1";
        }
    }
}
Advertisements