Magnus Rahl
Sep 25, 2009
  7102
(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
Customizing Property Lists in Optimizely CMS

Generic property lists is a cool editorial feature that has gained a lot of popularity - in spite of still being unsupported (officially). But if y...

Allan Thraen | Oct 2, 2022 | Syndicated blog

Content Delivery API – The Case of the Duplicate API Refresh Token

Creating a custom refresh provider to resolve the issues with duplicate tokens in the DXC The post Content Delivery API – The Case of the Duplicate...

David Lewis | Sep 29, 2022 | Syndicated blog

New Optimizely certifications - register for beta testing before November 1st

In January 2023, Optimizely is making updates to the current versions of our certification exams to make sure that each exam covers the necessary...

Jamilia Buzurukova | Sep 28, 2022

Optimizely community meetup - Sept 29 (virtual + Melbourne)

Super excited to be presenting this Thursday the 29th of September at the Optimizely community meetup. For the full details and RSVP's see the...

Ynze | Sep 27, 2022 | Syndicated blog

Preview multiple Visitor Groups directly while browsing your Optimizely site

Visitor groups are great - it's an easy way to add personalization towards market segments to your site. But it does come with it's own set of...

Allan Thraen | Sep 26, 2022 | Syndicated blog

The Report Center is finally back in Optimizely CMS 12

With Episerver.CMS.UI 12.12.0 the Report Center is finally re-introduced in the core product.

Tomas Hensrud Gulla | Sep 26, 2022 | Syndicated blog