Sitecore Faceting – Ordering Sitecore Global / Default Facets: Part-3

The third in a row about sitecore faceting and yet not the last one, I am planning to write two more posts on faceting after this. If you want to refer to the previous two posts below are the links,

1) Sitecore Faceting Novice Learnings: Part-1
2) Sitecore Faceting-List and Link Type Fields:Part-2

In this post I will share how can we Sort the Global (Default) facets available on the Search UI. Sitecore has most of such things either content managed (via Core Database) or via settings, sadly for ordering of global facets you will have to write your own processor. But it’s a simple piece of code so let’s check how to achieve it.

Below is the list and image of the default facets namely,

– Template
– Language
– Author
– Date Range

Sitecore Global Facets

which are available on any sitecore bucket item.

Sitecore uses a processor, “Sitecore.Buckets.Pipelines.Search.GetFacets.RunFacets” to get and display the facets. This processor can be found at \App_Config\Include\Sitecore.Bucket.config.

  
     
      

Now let’s writeup a class for a processor 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)
            {
                IEnumerable < IEnumerable < SitecoreUIFacet >  > facetListLists = (IEnumerable < IEnumerable < SitecoreUIFacet >  > )args.Result;
                IEnumerable < IEnumerable < SitecoreUIFacet >  >  newFacetListLists = facetListLists.OrderBy(fl => (fl.Count()  >  0) ? fl.FirstOrDefault().DisplayName : "");                
                args.Result = newFacetListLists;
            }
        }
        #endregion
}
}

To summarize the logic of above code, we check if there is a item attached to a facet (fl.Count() > 0) and then sort it based on “DisplayName” .

Add the FacetSortOrder processor after Sitecore’s RunFacets processor. So if you hit /sitecore/admin/showconfig.aspx it should reflect as follow,

Bucket-ShowConfig

Below is an xml that you can place in a Facet.config file for xml patching, make sure you put the Facet.config file in a folder under App_config/Include folder otherwise your processor will not get appended as Sitecore.Bucket.config falls after it chronologically.



  
    
      
        
      
    

  

Below image confirms the changed ordering of Sitecore’s Facets.

Global-Facets-Sorted

In my next post for faceting I will be writing about removing the global facets from the Search UI and displaying only the customs one as selected by a user.

Keep Sharing, Keep Caring!