Sitecore Faceting – Removing Global And Displaying Customs Facets: Part-4

We might want to have more control over the facets that are being displayed on the Search UI. When custom facets are defined they are mingled with the global ones and it is difficult to differentiate between them on the Search UI. The global facets are interpreted as sitecore default facets, but they are global ones meaning they appear on all bucket items. If you are following my Sitecore Faceting series then yes this is the fourth one talking about removing the global facets and displaying the ones that you choose.

Below image shows the custom facets mingled with global facets,

Sitecore Custom Facets Mingled

Sitecore Custom Facets Mingled

Let’s consider a hypothetical case where we need to display custom facets(News Release Date Range and News Category) related to our News bucket and which the content authors can take advantage of instead of using the global ones (Template, Language, Author, Date Range) which might not make sense to them.

The logic that we applied was to display only those facets which are selected on the “__facets” field available in the Indexing section.

Custom_Facets_-_Selected

Custom Facets Selected

 

We will take the same path as mentioned in my third post Sitecore Faceting – Ordering Sitecore Global / Default Facets: Part-3, let’s define a processor that will be patched after “Sitecore.Buckets.Pipelines.Search.GetFacets.RunFacets” processor. This processor can be found at \App_Config\Include\Sitecore.Bucket.config.

  
     
      

Now let’s writeup a class that will inherit from Sitecore.Buckets.Pipelines.Search.GetFacets.GetFacetsProcessor

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sitecore.Buckets.Pipelines.Search.GetFacets;
using Sitecore.ContentSearch.SearchTypes;
using Sitecore.Data.Items;
using Sitecore.Data.Fields;

namespace sc80up2.SitecoreEntities.Facet
{
    public class FacetSortOrder : GetFacetsProcessor
    {
        #region ...Public Methods
        
        public override void Process(GetFacetsArgs args)
        {
            if (args.Result != null)
            {
                //Get bucket item
                Item bucketItem = Sitecore.Data.Database.GetDatabase("master").GetItem(args.LocationFilter);
                if (bucketItem == null) return;

                //Facet field
                MultilistField fieldfacet = bucketItem.Fields["__facets"];
                if (fieldfacet == null) return;

                List<string> lstCustomFacets= new List<string>();
                //Get selected facet on a bucket
                foreach (var selectedfacet in fieldfacet.GetItems())
                {
                    if (selectedfacet.TemplateID.ToString() == TemplateIDs.FacetTemplateID)
                    {
                        //get facet field id, can be stored by comma seperated
                        if (selectedfacet.Fields["parameters"] != null && !string.IsNullOrEmpty(selectedfacet.Fields["parameters"].Value))
                        {
                            foreach (string str in selectedfacet.Fields["parameters"].Value.Split(','))
                            {
                                lstCustomFacets.Add(str);
                            }
                        }
                    }
                }

                IEnumerable<IEnumerable<SitecoreUIFacet>> facetListLists = (IEnumerable<IEnumerable<SitecoreUIFacet>>)args.Result;
                List<IEnumerable<SitecoreUIFacet>> newFacetListLists = new List<IEnumerable<SitecoreUIFacet>>();

                foreach (IEnumerable<SitecoreUIFacet> facetList in facetListLists)
                {
                    foreach (SitecoreUIFacet facet in facetList.ToList())
                    {
                        foreach (string str in lstCustomFacets)
                        {
                            if (facet.ID.ToLower() == str.ToLower())
                            {
                                newFacetListLists.Add(facetList);
                            }
                        }
                    }
                }
                args.Result = newFacetListLists.Distinct().OrderBy(fl => (fl.Count() > 0) ? fl.FirstOrDefault().Type : "");
            }
        }
        #endregion

    }

    public static class TemplateIDs
    {
        public const string FacetTemplateID = "{5C125B6D-C481-4C24-B5B9-9A23FE396BF0}";
    }
}

Code Explanation

Selected facets are first stored in a list (lstCustomFacets), these facets are read from the “__faceting” field. Iterate through all the facets that are passed to the processor via args and trying to match the selected one stored in the lstCustomFacets and return them from the method.
That was simple isn’t it? So the crux of this post is you can have your own processor for faceting.

Below image shows the final outcome where only selected (can be custom or global ones) being displayed on Search UI.

 

Outcome Sitecore Custom Facets

Outcome – Sitecore Custom Facets

Keep faceting and Sitecoring and yes do not forget to share your Sitecore Knowledge and learnings with the Sitecore Community.