Retrieve Page Tags (Taxonomy)

2025/05/05 2:23 PM

On XbK, I have a page content type with a field called Categories as taxonomy data type. I'm trying to retrieve the Categories tags associated with a page. I'm able to retrieve the tags GUIDs but the TagReference class only allows Identifier property. How do I convert the tag GUID to tag Name so the tag name actually displays? This is an example of how I successfully retrieve the Categories tags (GUIDs) associated with a page in my View:

@foreach (var pageItem in Model) { @foreach (var tag in pageItem.Categories) { @tag.Identifier } }

Environment

  • Xperience by Kentico version: [30.4.1]

  • .NET version: [8|9]

  • Execution environment: [SaaS|Private cloud (Azure/AWS/Virtual machine)]

  • Link to relevant Xperience by Kentico documentation

Tags:
Taxonomies ASP.NET Core Razor C#

Answers

2025/05/05 3:01 PM

You can use ITaxonomyRetriever to retrieve the taxonomy and its tags which will include all of the tag fields (name, display name, etc...).

This is detailed in our documentation under Retrieve information about tags and taxonomies.

// An instance of  ITaxonomyRetriever (e.g., obtained via dependency injection)
private readonly ITaxonomyRetriever taxonomyRetriever;

// Retrieves the general TaxonomyData object for a selected taxonomy
TaxonomyData taxonomyData = await taxonomyRetriever
  .RetrieveTaxonomy("ArticleCategory", "en");

// Retrieves data about the taxonomy from the TaxonomyData object
var taxonomy = taxonomyData.Taxonomy;
string taxonomyDisplayName = taxonomy.Title;
string taxonomyCodeName = taxonomy.Name;

// Retrieves the collection of all tags within the taxonomy
IEnumerable<Tag> tags = taxonomyData.Tags;

// Retrieves data about a tag within the taxonomy
Tag someTag = tags.FirstOrDefault();
string tagDisplayName = someTag.Title;
string tagCodeName = someTag.Name;

You can also just retrieve the tags you have identifiers for using the ITaxonomyRetriever.RetrieveTags() method.

2025/05/05 5:22 PM
Answer

I found an easy way and it works now! Here's the full reference: https://kenticode.com/retrieve-page-tags-in-xbk/

@foreach (var tag in pageItem.Tags) {
    var pageTag = await taxonomyRetriever.RetrieveTags([tag.Identifier], "en");
    var myTagTitle = pageTag.Select(t => t.Title).FirstOrDefault();
    @myTagTitle
}
2025/05/05 7:00 PM

I would highly caution against that approach - you are performing a classic N+1 query where N is the number of tags.

Instead of retrieving the details for each tag in a separate database query, you should retrieve them all together.

var tagIdentifiers = pageItem.Tags.Select(t => t.Identifier).ToList();

var tags = await taxonomyRetriever.RetrieveTags(tagIdentifiers, "en");
var tagsLookup = tags.ToDictionary(t => t.Identifier);

foreach(var identifier in tagIdentifiers)
{
  string displayName = tagsLookup.TryGetValue(identifier, out var tag)
    ? tag.DisplayName
    : "";
}

Additionally, I noticed your sample code looks like Razor. You should not be doing database calls in your Razor view.

Your view should have access to a view model which was prepared by a View Component or MVC Controller.

You can learn more about these recommendations and better application architecture in our guides.

To answer this question, you have to login first.