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();
1. Adding a Taxon to the Taxonomy:
2. Adding a Taxon to a Taxon:
Note that we are alo setting the properties of the taxonomy and the taxa, and invoke SaveChanges() in the end.
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:
Once we have the Taxa, or Taxonomy instance, the content categorization could begin. This will be shown in a separate article though.