Episerver is great at a lot of things but I think it's tagging does let it down a bit without having to buy one of the bulk upgrade products.   In Episerver all you can do is define a single global category that is available to all page templates.  We had a requirement to create different categories based on different page types.  If we added a global category then editors would be able to add in wrong categories for their page type.

I found a good article here that solved a similar problem, however when I implemented it I had a lot of issues around saving.  With the proposed solution, if you clicked save twice the data would be lost.  I made an update to only publish but this made other fields break.   In my site I have created a new category section as shown :

Screenshot_5 When I define my property (I'll get to that later)

I use the 'HelpText' attribute to define which part of the category tree the property should use, as seen below the HelpText matches the name I've set-up in my categories.

One important thing to note about this process is that even though the type is of  'PropertyCategoryCheckboxList' the property is string.  What this means is when we iterate through the control list later any selected ID will have to be stored in a comma separated string. First define the property class :

Define the control class:

In the code above we're :

  • Getting the value of help text and then querying the category list to get all the children
  • Getting any existing user selection (stored in ProperyData.Value as a comma separated string) and then calling GetCategoryIds
  • GetCategoryIds passes that string into a list of Id's
  • Add the controls to the page and select any of the entries that have been previously selected.

The last part of the puzzle is ApplyEditChange and storing the selected values, which was the main issue I had with the code I found on-line.

The big difference with my code that I'm storing is the users selection is stored using the PropertyData.Value rather than creating a clone and saving that clone and creating all the nastiness that causes.  All the code is doing :

  • Loop through the checkbox list displayed and store anything that is ticked in categoryIdList
  • Iterate through cateogryIdList and create a string of Id's separated by a comma
  • Store this string using PropertyData.Value

If the code above works in you should have a checkbox list on your page type that pulls in all the child categories.

Screenshot_6 

You can find some demo code here.