Hierarchical Taxonomy API

Hierarchical Taxonomy API - Working with Hierarchical Taxonomy and Taxons programmatically

Creating a Hierarchical Taxonomy and a new Taxon

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

//Taxonomies namespaces
using Telerik.Sitefinity.Taxonomies;
using Telerik.Sitefinity.Taxonomies.Model;
..
..
//Create a new hierarhical taxonomy using the TaxonomyManager class
TaxonomyManager manager = TaxonomyManager.GetManager();
var tax = manager.CreateTaxonomy<HierarchicalTaxonomy>();
tax.Name = "News";
tax.TaxonName = "News";
tax.Description = "Taxonomy which will classify the news on our web site";
//Create a new taxon and add it to the taxonomy - Europe News
var rootTaxonNews = manager.CreateTaxon<HierarchicalTaxon>();
rootTaxonNews.Title = "From Europe";
rootTaxonNews.Name = "European News";
rootTaxonNews.Description = "This category holds the news from Europe";
tax.Taxa.Add(rootTaxonNews);
//Create two sub-taxa and add them to the European News taxon.
//1
var taxon1 = manager.CreateTaxon<HierarchicalTaxon>();
taxon1.Title = "Politics";
rootTaxonNews.Name = "Politics";
taxon1.Description = "This category holds the Politics news";
rootTaxonNews.Subtaxa.Add(taxon1);
//2
var taxon2 = manager.CreateTaxon<HierarchicalTaxon>();
taxon2.Title = "Entertainment";
taxon2.Name = "Entertainment";
taxon2.Description = "This category holds the Entertainment news";
rootTaxonNews.Subtaxa.Add(taxon2);
//Create a new taxon and add it to the taxonomy - US News
var rootTaxonUSNews = manager.CreateTaxon<HierarchicalTaxon>();
rootTaxonUSNews.Title = "From USA";
rootTaxonUSNews.Name = "USA News";
rootTaxonUSNews.Description = "This category holds the news from USA";
tax.Taxa.Add(rootTaxonUSNews);
//Save all changes done up to now.
manager.SaveChanges();

Explanation:

The code produces Taxonomy with the following structure:

Figure 1:Hierarchical Taxonomy produced by the code example
Creating taxonomy begins with the use of TaxonomyManager. When creating Hierarchical taxonomies, CreateTaxonomy() method is used with HierarchicalTaxonomy type. Furthermore, all taxa added to this taxonomy should be of type HierarchicalTaxon. In this example, we are showing two operations as well -

1. Adding a Taxon to the Taxonomy:

tax.Taxa.Add(rootTaxonNews);

2. Adding a Taxon to a Taxon:

rootTaxonNews.Subtaxa.Add(taxon1);

Note that we are alo setting the properties of the taxonomy and the taxa, and invoke SaveChanges() in the end.

Here's how the result should look like in the Classifications screen:

Figure 2:Hierarchical Taxonomy produced by the code example

Hierarchical Taxonomies and Taxons

Finding Taxonomy:

TaxonomyManager manager = TaxonomyManager.GetManager();
var taxonomy = manager.GetTaxonomies<HierarchicalTaxonomy>().Where(t => t.Name == "News").SingleOrDefault();

Taxonomy could be also get by ID, using the GetTaxonomy(Guid id) method. Searching for a Taxon or Taxa is very similar to searching for a Taxonomy:

var taxa = manager.GetTaxa<HierarchicalTaxon>().Where(t => t.Name == "USA News").Single();

Once we have the Taxa, or Taxonomy instance, the content categorization could begin. This will be shown in a separate article though.

Next steps

+1-888-365-2779
sales@sitefinity.com

Related topics:

Feedback

How useful is this article?

Tell us more

Submit
Your message was successfully sent.

We appreciate your feedback.

Your message could not be sent.

OK