Computed Field based on Parent Name

No comments

In most of the case we might need to consider or facet based on parent element of a category.

  • Products
    • Sports
      • Football
      • Cricket
      • Tennis
    • Electronics
      • Mobile
      • Laptop
      • Television
    • Apparels
      • Men’s wear
      • Women’s wear

Now lets say you’re using a Tree List field in a template to assign all types of Products and you’ve to filter based on 1st level of categories i.e, Sports – Electronics and Apparels and store in index.

Lets see how it is done,

    1. Create Computed Categories class to store all category details with its parent name

      public class Products : ComputedField
          {
              public override ID TagsSelector
              {
                  get
                  {
                      return new ID("{DC222D36-302A-492C-BFCA-71064B3D8B37}");
                  }
              }
          }
      
          public class SportsCategory : ComputedField
          {
              public override ID TagsSelector
              {
                  get
                  {
                      return new ID("{DC222D36-302A-492C-BFCA-71064B3D8B37}");
                  }
              }
              public override string ParentName
              {
                  get
                  {
                      return "Sports";
                  }
      
              }
      
          }
      
          public class ElectronicsCategory : ComputedField
          {
              public override ID TagsSelector
              {
                  get
                  {
                      return new ID("{DC222D36-302A-492C-BFCA-71064B3D8B37}");
                  }
              }
              public override string ParentName
              {
                  get
                  {
                      return "Electronics";
                  }
              }
          }
      
          public class ApparelsCategory : ComputedField
          {
              public override ID TagsSelector
              {
                  get
                  {
                      return new ID("{DC222D36-302A-492C-BFCA-71064B3D8B37}");
                  }
              }
              public override string ParentName
              {
                  get
                  {
                      return "Apparels";
                  }
              }
          }
       
    2. Resolve in computed field based on its parent name
 public abstract class ComputedField : IComputedIndexField
    {
        public virtual ID TagsSelector { get; set; }

        //Keeping it simple - Category Name
        public virtual string ParentName { get; set; }

        public virtual object ComputeFieldValue(IIndexable indexable)
        {
            var indexableItem = indexable as SitecoreIndexableItem;

            if (string.IsNullOrEmpty(ParentName))
                return indexableItem == null || TagsSelector.IsNull ? null : string.Join("|", indexableItem.Item.GetMultiListValues(TagsSelector).Select(tag =>  (tag.Fields["Name"] != null ? tag.Fields["Name"].Value : tag.Name)));
            else
                return indexableItem == null || TagsSelector.IsNull ? null : indexableItem.Item.GetMultiListValues(TagsSelector).Where(x => x.Parent.Name == ParentName).Select(tag => tag.Name).ToList();
        }

        public string FieldName { get; set; }
        public string ReturnType { get; set; }
    }

Finally add in config


        
          
            
              
                
              
              
                
              
              
                
              
              
                
              

            
          
          
            Namespace.ProductsCategory, Namespace
            Namespace.SportsCategory, Namespace
            Namespace.ApparelsCategory, Namespace
            Namespace.ElectronicsCategory, Namespace
          
        
      

Now data will get indexed based on parents.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.