Views: 6735
Number of votes: 1
Average rating:

Automatic Sub Category Property Type

I had the need to categorize pages in several different “dimensions”, grouped as subcategories. I wanted to display these categories in different properties, but I didn’t want to display the whole category tree in each property (it’s quite large), or have the editor set all the dimensions in the same property. For this purpose I wanted to display only configurable part of the category tree.

Solution: PropertySubCategory

This property type is a simple extension of the PropertyCategory which finds the subtree “root” based on the name of the property. Another way could have been to use the default value, but that only allows the numerical category ID which could be cumbersome to find.

Code:

   1: /// <summary>
   2: /// Shows a subtree of the category tree based on the name of the property.
   3: /// If the property name is aaa-NN or aaa-SSS the number NN or the string SSS
   4: /// is used to find a category and the subtree below that category
   5: /// (if found) is displayed.
   6: /// </summary>
   7: [PageDefinitionTypePlugIn]
   8: public class PropertySubCategory : PropertyCategory
   9: {
  10:     public override System.Collections.IList GetAvailableCategories()
  11:     {
  12:         if (this.Name != null)
  13:         {
  14:             // Get the category name or ID from the property name
  15:             int startIndex = this.Name.LastIndexOf('-');
  16:             if ((startIndex > 0) && (this.Name.Length > ++startIndex))
  17:             {
  18:                 int rootId;
  19:                 string rootName = this.Name.Substring(startIndex, this.Name.Length - startIndex);
  20:  
  21:                 // Get the category
  22:                 EPiServer.DataAbstraction.Category subRoot;
  23:                 if (int.TryParse(rootName, out rootId))
  24:                 {
  25:                     subRoot = EPiServer.DataAbstraction.Category.GetRoot().FindChild(rootId);
  26:                 }
  27:                 else
  28:                 {
  29:                     subRoot = EPiServer.DataAbstraction.Category.GetRoot().FindChild(rootName);
  30:                 }
  31:  
  32:                 // Return the category child categories if the category was found
  33:                 if (subRoot != null)
  34:                 {
  35:                     return subRoot.GetList();
  36:                 }
  37:             }
  38:         }
  39:         // No category specified or found, fall back to default behaviour
  40:         return base.GetAvailableCategories();
  41:     }
  42: }

Usage

So say I have a category tree which is [Root] – Dimensions – Technologies – [Available categories for technology here]. I then create a property called “Category-Technologies” (or “Category-14” if 14 is the ID of Technologies) of the sub category property type. Then only the subtree below Technologies will be displayed in edit mode. Neat, isn’t it?

Sep 25, 2009

Ger Groot
(By Ger Groot, 1/23/2013 5:58:22 PM)

Hi, I'm trying to get this to work in EPiServer 7, can you point me in the right direction?

The only difference is that i'm trying to list the subcategories which are based on the name of the property as defined in the ContentType.

[PageDefinitionTypePlugIn]
public class PropertySubCategory : PropertyCategory
{
public override IList GetAvailableCategories()
{
if (this.Name != null)
{
var subCategory = EPiServer.DataAbstraction.Category.Find(this.Name);

if (subCategory != null)
{
return subCategory.GetList();
}
}

return base.GetAvailableCategories();
}
}

Please login to comment.