Try our conversational search powered by Generative AI!

Magnus Rahl
Sep 25, 2009
  7586
(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
Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog