Let’s assume you developed a widget using Xperience by Kentico (XbK) that allows you to display various pages and you need to display their associated taxonomy (Tags).
Assuming your page content type has a field called Tags as taxonomy data type, you can easily display the taxonomy GUID stored in the page Tag field using the following code in the View:
@foreach (var pageItem in Model) { <h1>@tag.PageTitle</h1> <p>@tag.PageDescription</p> @foreach (var tag in pageItem.Tags) { <p>Tags: @tag.Identifier</p> } }
All good, but because the TagReference class only allows Identifier property you need to find a way to display the actual taxonomy field Title associated with the Tag GUID.
First, make sure you inject the ITaxonomyRetriever at the top of the View.
@inject ITaxonomyRetriever taxonomyRetriever;
Next, use the ITaxonomyRetriever to retrieve the page taxonomy and its tags which will include all of the tag fields (ID, Title, Name).
@foreach (var pageItem in Model) { <h1>@tag.PageTitle</h1> <p>@tag.PageDescription</p> @foreach (var tag in pageItem.Tags) { var pageTag = await taxonomyRetriever.RetrieveTags([tag.Identifier], "en"); var myTagID = pageTag.Select(t => t.ID).FirstOrDefault(); // Tag ID var myTagTitle = pageTag.Select(t => t.Title).FirstOrDefault(); // Tag Title <p>Tags: <a href="/Tags?tag=@myTagID">@myTagTitle</a></p> } }
References:
Retrieve information about tags and taxonomies
Kentico Community Portal
Class TagReference