Flat taxonomies API

Create flat taxonomies and new taxons

Sitefinity CMS exposes manager classes for every type of content. This is the case for taxonomies, as well. Consider the following example:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Telerik.Sitefinity.Model;
using Telerik.Sitefinity.Taxonomies;
using Telerik.Sitefinity.Taxonomies.Model;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Taxonomies.FlatTaxonomies.FlatTaxonomyAPI
{
   public partial class FlatTaxonomyAPI
   {
       public static void CreateFlatTaxonomy()
       {
           //gets an instance of the TaxonomyManager
           TaxonomyManager manager = TaxonomyManager.GetManager();

           //creates flat taxonomy
           var tax = manager.CreateTaxonomy<FlatTaxonomy>();
           tax.TaxonName = "Meals";
           tax.Name = "Meals"; // .Name will be automatically set by the system if not set explicitly
           tax.Title = "Meals";
           tax.Description = "Meals Description";

           //creates three taxons for the taxonomy
           //1
           var taxon = manager.CreateTaxon<FlatTaxon>(Guid.NewGuid());
           taxon.Title = "Breakfast";
           taxon.Name = "Breakfast"; // .Name will be automatically set by the system if not set explicitly
           taxon.Description = "This tag categorizes the Breakfast";
           taxon.UrlName = new Lstring(Regex.Replace("Breakfast", @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-").ToLower());
           tax.Taxa.Add(taxon);
           manager.SaveChanges();

           //2
           taxon = manager.CreateTaxon<FlatTaxon>(Guid.NewGuid());
           taxon.Title = "Lunch";
           taxon.Name = "Lunch"; // .Name will be automatically set by the system if not set explicitly
           taxon.Description = "This tag categorizes the Lunch";
           taxon.UrlName = new Lstring(Regex.Replace("Lunch", @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-").ToLower());
           tax.Taxa.Add(taxon);
           manager.SaveChanges();

           //3
           taxon = manager.CreateTaxon<FlatTaxon>(Guid.NewGuid());
           taxon.Title = "Dinner";
           taxon.Name = "Dinner"; // .Name will be automatically set by the system if not set explicitly
           taxon.Description = "This tag categorizes the Dinner";
           taxon.UrlName = new Lstring(Regex.Replace("Dinner", @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-").ToLower());
           tax.Taxa.Add(taxon);
           manager.SaveChanges();
       }
   }
}

You are working with the TaxonomyManager class, which is the manager class for taxonomies. It has helper methods for creating a taxonomy: CreateTaxonomy(), managing taxonomies, and creating taxons.

NOTE: The new taxonomy must have a name, title, root taxon name, and description. The Name gets or sets the programmatic name of the taxon, whereas the Title gets or sets the display title of the taxonomy.

After setting the taxonomy properties, you create the first taxon and set its properties as well.
The taxon is added to the taxonomies taxa (taxon collection) via the taxonomy class helper method Add(Taxon taxon).

Finally, you call the SaveChanges(), so that a transaction with all the changes is created and run.

Find flat taxonomies and taxons

To find a taxonomy:

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

namespace SitefinityWebApp
{
   public class FindTaxonomy
   {
       public FlatTaxonomy FindATaxonomy()
       {
           TaxonomyManager manager = TaxonomyManager.GetManager();

           var taxonomy = manager.GetTaxonomies<FlatTaxonomy>().Where(t => t.Title == "Meals").Single();

           return taxonomy;

       }
   }
}

You can also get a taxonomy by ID using the GetTaxonomy(Guid id) method. Searching for a taxon or taxa is very similar to searching for a taxonomy:

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

namespace SitefinityWebApp
{
   public class GetTaxonomyById
   {
       public FlatTaxon GetTaxonomyId(Guid taxaId)
       {
           TaxonomyManager manager = TaxonomyManager.GetManager();
           var taxa = manager.GetTaxa<FlatTaxon>().Where(t => t.Id == taxaId).Single();
           return taxa;

       }
   }
}

Once you have the taxa or taxonomy instance, you can associate them with a content item.

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.