Display Categories Using Transformation

Julian C. asked on July 19, 2018 18:49

I know this topic has been debated few years back but still there is no simple solution to it.

I'm trying to display the Categories associated to a page without creating a custom transformation function described here

Here is how I want it displayed: Categories: Category One Category Two | Tags: Tag One Tag Two

I managed to have the Tags displayed. But after trying all kinds of macros for Categories the only option left is the article above which I want to avoid.

The question is, is there any way to have the Categories related to a page displayed on the page next to the Tags using a simple macro in transformation?

Recent Answers


Zach Perry answered on July 19, 2018 18:53 (last edited on December 10, 2019 02:31)

Have you tried something like {%CurrentDocument.Categories.ApplyTransformation("sometransform")|(identity)GlobalAdministrator%}

0 votesVote for this answer Mark as a Correct answer

Julian C. answered on July 19, 2018 19:12

Zach, not sure what transformation to use for "ApplyTransformation("sometransform")".

Here is my custom transformation for the page where the Tag macro works fine:

<div class="row">
    <div class="col-lg-12">
    <p><b><a href="<%# GetDocumentUrl() %>"><%# Eval("MyTitle") %></a></b></p>
    <p><%# Eval("MySummary") %></p>
    </div>
    <div class="col-lg-12">
    <%# BlogFunctions.GetDocumentTags(Eval("DocumentTagGroupID"), Eval("DocumentTags"), "/Tag") %>
    </div>
</div>
0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on July 19, 2018 19:26 (last edited on December 10, 2019 02:31)

Technically you can do directly:

{% CurrentDocument.Categories.DisplayNames|(identity)GlobalAdministrator%}

0 votesVote for this answer Mark as a Correct answer

Julian C. answered on July 19, 2018 19:41

Peter, when using the first option I get this message displayed: CMS.DataEngine.CollectionPropertyWrapper`1[CMS.DataEngine.BaseInfo]

When using second option I get this error: error CS1061: 'IInfoObjectCollection' does not contain a definition for 'Transform' and no extension method 'Transform' accepting a first argument of type 'IInfoObjectCollection' could be found (are you missing a using directive or an assembly reference?)[TempITemplate.Template]

BTW, I'm using ASCX transformation type.

First option:

<%# CurrentDocument.Categories.DisplayNames %>

Second Option:

<%# CurrentDocument.Categories.Transform("ID: {#CategoryId#} Name: {#DisplayName#}<br>") %>
0 votesVote for this answer Mark as a Correct answer

Zach Perry answered on July 19, 2018 20:54

Looks like you are using a repeater, so macros with currentdocument won't work, they would pull back the categories for whatever page the repeater is on, not the document in the repeater.

Is the repeater using a custom query?

0 votesVote for this answer Mark as a Correct answer

Julian C. answered on July 19, 2018 21:28

Zach, yes, I'm using a repeater with two transformations (one for listing the items and one for a selected item). And, no, I'm not using custom query.

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on July 19, 2018 21:41

Sorry didn't notice it. You can call macro resolver <%# CMS.MacroEngine.MacroResolver.Resolve("{% your macro here %}") %>. There is a mix between ACSX and Text/XML transformations. What I've shown above is how to list categories for the current document.

So you are listing documents and you want to get categories for each one? Correct? This might be costly because for each document there will be a sql call to get the categories (which you probably dont want). So your query that gets documents should get categories for them as well as well as the tags.

0 votesVote for this answer Mark as a Correct answer

Julian C. answered on July 19, 2018 22:47 (last edited on December 10, 2019 02:31)

Peter, I tried both macros you recommended using the MacroResolver without any luck.

<%# CMS.MacroEngine.MacroResolver.Resolve("{% CurrentDocument.Categories.DisplayNames|(identity)GlobalAdministrator%}") %>

It needs to be an easy way to display Categories associated to a page. It is just a simple request. I don't understand why Kentico makes it so difficult. This was done in WordPress in a matter of seconds. Very frustrating.

1 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on July 20, 2018 01:37 (last edited on July 20, 2018 01:50)

Macro wont work here, because they will show categories of the current document, but you want categories for documents rendered by your transformation.

I know sometimes it might look a bit weird, but there are reasons why certain things are done in a certain way. Because you are using ACSX transformation you need to implement a custom transformation function, here is a good example.

P.S. The problem with this approach as I explained above - it will cause a SQL query to get categories for each document, i.e. if you displaying 10 documents - you will get 10 extra SQL queries. The alternative approach would be to get documents and their categories in one query.

0 votesVote for this answer Mark as a Correct answer

Julian C. answered on July 20, 2018 17:21

Thanks Peter. So, you're suggesting not to use a repeater to display a list of pages with their associated Title, Summary, Tags and Categories? How else can I display this format? I don't understand what you meant by "get documents and their categories in one query".

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on July 20, 2018 19:41

Try it with the repeater,but each component you add to your page/transformation might eventually query SQL database. So I am just saying you have to be aware of it. I might slow your page.

0 votesVote for this answer Mark as a Correct answer

Julian C. answered on July 20, 2018 23:42 (last edited on December 10, 2019 02:31)

So, I found a way to have the Categories show to a single page (selected page transformation) but not to the list of pages (as index) or search result page.

Here is what I used:

<%# CMS.MacroEngine.MacroResolver.Resolve("{% Documents[NodeAliasPath].Categories.DisplayNames |(identity)GlobalAdministrator%}") %>

Also, the Categories are showing without any formatting, no comma between the categories. How would I add a comma between the categories using the above solution? And would it even be possible to have the Categories as links?

0 votesVote for this answer Mark as a Correct answer

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