Magnus Rahl
Sep 25, 2009
  7622
(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
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024

Frontend Hosting for SaaS CMS Solutions

Introduction Now that CMS SaaS Core has gone into general availability, it is a good time to start discussing where to host the head. SaaS Core is...

Minesh Shah (Netcel) | Jul 20, 2024

Optimizely London Dev Meetup 11th July 2024

On 11th July 2024 in London Niteco and Netcel along with Optimizely ran the London Developer meetup. There was an great agenda of talks that we put...

Scott Reed | Jul 19, 2024