Document Category Order in Macro

Ryan Kilgore asked on June 17, 2022 23:34

I'm working on displaying the Categories that have been applied to a given page, but I'm having issues with ordering the collection in the page transformation when using a macro.

Here's the macro in question: {% CurrentDocument.Categories.OrderBy("CategoryParentID,CategoryOrder").DisplayNames |(identity)GlobalAdministrator%}

While this returns the correct category data for what I've selected for the page, I can't get OrderBy to affect the output order in any way. The output is always ordered alphabetically (by what appears to be CategoryDisplayName).

I noticed when I tested a similar macro on all Categories using SiteObjects, the output is ordered exactly as how I would like it: {% SiteObjects.Categories.OrderBy("CategoryParentID,CategoryOrder").DisplayNames |(identity)GlobalAdministrator%}

Is there any particular reason the categories pulled from CurrentDocument aren't getting ordered? Is that object missing some additional context to utilize CategoryParentID and CategoryOrder?

When I use the first macro in the Macro Console, both attributes are available in the virtual data, so I'm currently at a loss on why OrderBy doesn't appear to be doing anything.

Any insight would be appreciated!

Correct Answer

Juraj Ondrus answered on June 20, 2022 07:57

I would recommend using the SQL debug to see what SQL query is generated.
I would say that the second macro works because you are listing through the category object type which is getting the data from the CMS_Category DB table - so you can order them by those column names. But, when a category is assigned to a page, this information is stored in a binding table CMS_DocumentCategory - and as you can see, it does not have the CategoryOrder or CategoryParentID columns. This means, this information is not available with the current document.

To achieve your need I would recommend creating a custom macro. Then, in it's C# logic get the current document's categories and sort them and order them as you want by comparing it with the category object.

0 votesVote for this answer Unmark Correct answer

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