Limit Multiple Categories form control by Parent

Paul Truman asked on January 11, 2023 16:38

Hey, I've been taking a look at the Form Control code for the Multiple Categories selector in order to limit the results by a parent field. So basically I can add a field for a parent folder, and the Form Control will only show the child categories (and not everything).

Unfortunately I can't seem to find a way that works. Can anyone point me in the right direction?

Correct Answer

Brenden Kehren answered on January 11, 2023 17:19

I'd suggest NOT using the multiple category selector. I'd suggest uing the Object Selector. This will allow you to do some custom work in the UI without writing custom code for a form control. You can set the following values in the Advanced section of the control:

  • Object type = cms.category
  • Return column name = CategoryGUID (suggest using this so they are the same across environments)
  • Object site name = #curentsite
  • Selection mode = Multiple
  • Where condition = CatgegoryName LIKE 'StartOfCodeName.%'(suggest using the code name field and prefix it like a namespace as the category ID and category path can change very easy and are not the same in all environments.
  • Order by CategoryDisplayName

This should allow you to get what you want. Keep in mind how you structure your category tree will make a difference, kind of. We use it for organizational structure but base all our look ups on the code name and how we prefix them. For example, the following is a list of News categories with the first value being the display name and the second value being the code name.

- News, main.news
-- General, news.general
-- Technology, news.technology
--- Computer Games, news.technology.computergames
--- Computer Programming, news.technology.computerprogramming
-- Lifestyle, news.lifestyle
--- Outdoors, news.lifestyle.outdoors
--- Travel, news.lifestyle.travel
---- Local, news.lifestyle.travel.local
---- International, news.lifestyle.travel.international

So in the above example, if I wanted to get all the Lifestyle categories, I'd change my WhereCondition to `CatgegoryName LIKE 'news.lifestyle.%' and it would return the following (in a nested format):

--- Outdoors, news.lifestyle.outdoors
--- Travel, news.lifestyle.travel
---- Local, news.lifestyle.travel.local
---- International, news.lifestyle.travel.international

There are may ways to do this but we've found this to be the most robust way to keep categories from breaking and allow users to enter their new categories without causing a things to break or additional dev time.

1 votesVote for this answer Unmark Correct answer

Recent Answers


Brenden Kehren answered on January 11, 2023 16:41

What version are you working with? Where would this form control be utilized? Page Builder or Kentico UI?

0 votesVote for this answer Mark as a Correct answer

Paul Truman answered on January 11, 2023 16:44

Hey Brenden, Thanks for the reply :) I'm working on Xperience 13 and the form control would be used in the Kentico UI.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.