Retrieve Page Tags and Categories on K13

Julian C. asked on November 14, 2023 18:39

I'm having issues retrieving page Tags and Categories to display on pages for Kentico 13 (core). I cannot find anything helpful other than retrieving Tags Groups: https://docs.xperience.io/13api/content-management/tags

I wish Kentico would provide more examples for basic features.

Any help would be much appreciated.

Recent Answers


Brenden Kehren answered on November 14, 2023 18:56

Check out the documentation for Categories here.

0 votesVote for this answer Mark as a Correct answer

Julian C. answered on November 15, 2023 15:53

Thank you Brenden, but my issue was with retrieving Tags and Categories on pages not how to create Tags and Categories.

I managed to find a way to retrieve Tags on pages but I'm still struggle with Categories.

Here's how I retrieved page Tags:

Code

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.");
    }

Code

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on November 15, 2023 16:52

If you read into the API examples a bit more all you really need is the namespace and class to dig into the available methods for that class. For example, if you learn how to add a page to a category, you'll find out you can use the DocumentCategoryInfo.Provider class and the .Get() method to retrieve document info by category ID or document ID.

The same is true with Tags, check out how to add a tag to a page. The only difference with Tags, is you first need a Tag Group (TagGroupInfo.Provider) to be able to .Get() all the tags (TagInfo.Provider.Get()) in a group. From there you can then get pages associated with tags in that given group.

I don't know specifically what your use case is but the API examples provide quite a bit of information for you to be able to get this information. Maybe provide more details as to your use case.

0 votesVote for this answer Mark as a Correct answer

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