Hierarchical taxonomies API

Create a Hierarchical taxonomy and a new Taxon

Sitefinity CMS exposes manager classes for every type of content.
The following code sample creates a hierarchical taxonomy and a taxon:

C#
using System.Text.RegularExpressions;
using Telerik.Sitefinity.Model;
using Telerik.Sitefinity.Taxonomies;
using Telerik.Sitefinity.Taxonomies.Model;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Taxonomies.HierarchicalTaxonomies.HierarchicalTaxonomyAPI
{
   public partial class HierarchicalTaxonomyAPI
   {
       public void CreateHierarchicalTaxonomyAndTaxon()
       {
           //gets an instance of the taxonomy manager
           TaxonomyManager manager = TaxonomyManager.GetManager();

           //creates hierarchical taxonomy - Language family
           var taxonomy = manager.CreateTaxonomy<HierarchicalTaxonomy>();
           taxonomy.Title = "Language family";
           taxonomy.Name = "Language-family"; // .Name will be automatically set by the system if not set explicitly
           taxonomy.TaxonName = "Language family";

           //creates a new taxon and adds it to the taxonomy - Language Groups
           var rootTaxonCateg = manager.CreateTaxon<HierarchicalTaxon>();
           rootTaxonCateg.Title = "Language Groups";
           rootTaxonCateg.Name = "Language-Groups";
           rootTaxonCateg.UrlName = new Lstring(Regex.Replace("Language Groups", @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-").ToLower());
           rootTaxonCateg.Description = "Language groups description";
           rootTaxonCateg.Taxonomy = taxonomy;
           taxonomy.Taxa.Add(rootTaxonCateg);

           //creates two sub-taxa and adds them to the Language Groups taxon.
           //1
           var taxonSubCateg = manager.CreateTaxon<HierarchicalTaxon>();
           taxonSubCateg.Title = "Languages";
           taxonSubCateg.Name = "Languages";
           taxonSubCateg.UrlName = new Lstring(Regex.Replace("Languages", @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-").ToLower());
           taxonSubCateg.Description = "Languages description";
           taxonSubCateg.Parent = rootTaxonCateg;
           taxonSubCateg.Taxonomy = taxonomy;
           rootTaxonCateg.Subtaxa.Add(taxonSubCateg);

           //2
           var taxonChildSubCateg = manager.CreateTaxon<HierarchicalTaxon>();
           taxonChildSubCateg.Title = "Dialects";
           taxonChildSubCateg.Name = "Dialects";
           taxonChildSubCateg.UrlName = new Lstring(Regex.Replace("Dialects", @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-").ToLower());
           taxonChildSubCateg.Description = "Dialects description";
           taxonChildSubCateg.Parent = taxonSubCateg;
           taxonChildSubCateg.Taxonomy = taxonomy;
           taxonSubCateg.Subtaxa.Add(taxonChildSubCateg);

           //Save all changes done up to now.
           manager.SaveChanges();
       }
   }
}

The code creates a taxonomy with the following structure:

Hierarchical taxonomies API Result

You first get the TaxonomyManager. When creating hierarchical taxonomies, CreateTaxonomy() method is used with HierarchicalTaxonomy type. All taxa added to this taxonomy must be of type HierarchicalTaxon. In this example, there are two operations - adding a taxon to the taxonomy and adding a taxon to a taxon.

You are also setting the properties of the taxonomy and the taxa, and invoke SaveChanges() in the end.

Find a hierarchical taxonomy

To find a hierarchical taxonomy by its name, use the following code sample:

C#
using System.Linq;
using Telerik.Sitefinity.Taxonomies;
using Telerik.Sitefinity.Taxonomies.Model;

namespace SitefinityWebApp
{
   public class FindHierarchicalTaxonomy
   {
       public Taxon FindTaxonomyByName()
       {
           var manager = TaxonomyManager.GetManager();
           var categoriesTaxonomy = manager.GetTaxonomy<HierarchicalTaxonomy>(TaxonomyManager.CategoriesTaxonomyId);
           var categoryTaxa = categoriesTaxonomy.Taxa.Where(t => t.Title == "Language Groups").FirstOrDefault();

           return categoryTaxa;

       }
   }
}

First, you get the taxonomy by id, then, you search its taxa by name.

Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.