Magnus Rahl
Sep 25, 2009
  7459
(1 votes)

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

Comments

Ger Groot
Ger Groot Jan 23, 2013 04:58 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.
Latest blogs
Configured Commerce - Introduction to Long-Term Support (LTS) Releases

First off, for those who have not had a chance to meet me yet, my name is John McCarroll, and I am the Technical Product Manager for the Optimizely...

John McCarroll | Sep 29, 2023

Auto-translate with OpenAI in Optimizely CMS

You can now auto-translate content using your favorite online AI service, inside the old trustworthy Episerver.Labs.LanguageManager!

Tomas Hensrud Gulla | Sep 29, 2023 | Syndicated blog

Vulnerability in CMS 12 shell module configuration

Introduction A potential security vulnerability has been identified in Optimizely CMS 12, triggered by a certain shell module configuration. To be...

Magnus Rahl | Sep 28, 2023

AI-Assistant: The 'Change Tone' Shortcut

The AI-Assistant for Optimizely is constantly evolving, adjusting, and transforming to meet your digital needs, providing a cutting-edge advantage...

Luc Gosso (MVP) | Sep 27, 2023 | Syndicated blog