Display Page Tags

Kentico 13 display page tags

In Kentico 13 and Xperience, you can retrieve page tags by accessing the DocumentTags property of the TreeNode object.

Replace “/YourPagePath” with the actual path of the page whose tags you want to display.

This example code uses Kentico’s DocumentHelper to retrieve a specific page by its path and then accesses the DocumentTags property of the TreeNode object to get the tags associated with that page.

Make sure you have the necessary references and proper Kentico Xperience setup to use the Kentico API and TreeNode class.

Also, ensure that you have appropriate access rights to retrieve the page information.

using CMS.DocumentEngine;
using System;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        // Replace 'NodeAliasPath' with the path of your page
        string pagePath = "/YourPagePath";

        // Get the specific page by its path
        TreeNode page = DocumentHelper.GetDocuments()
            .Path(pagePath)
            .OnCurrentSite()
            .FirstOrDefault();

        if (page != null)
        {
            // Get tags associated with the page
            string[] pageTags = page.DocumentTags.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

            if (pageTags.Any())
            {
                Console.WriteLine("Tags for the page:");
                foreach (var tag in pageTags)
                {
                    Console.WriteLine(tag);
                }
            }
            else
            {
                Console.WriteLine("No tags found for the page.");
            }
        }
        else
        {
            Console.WriteLine($"Page with path '{pagePath}' not found.");
        }
    }
}

P.S.
Here’s another method of displaying page tags using TagInfoProvider.GetTags(DocumentID) straight in your .cshtml page:

@using CMS.Taxonomy;
var myTags = TagInfoProvider.GetTags(DocumentID);
@foreach (var tag in myTags)
{
<a href="/Tags?tag=@tag.TagID">@tag.TagName</a>
}