Flat Taxonomy API
Flat Taxonomy API - Working with Flat Taxonomy and Taxons programmatically
Creating a Flat Taxonomy and a new Taxon
Sitefinity exposes manager classes for every type of content. This is the case for Taxonomies, as well. Consider the following example:
//Taxonomies namespaces
using Telerik.Sitefinity.Taxonomies;
using Telerik.Sitefinity.Taxonomies.Model;
..
Guid tag1 = new Guid("913E28AA-0DE2-470d-9C3F-C726B973EE1D");
//
TaxonomyManager manager = TaxonomyManager.GetManager();
var tax = manager.CreateTaxonomy<FlatTaxonomy>();
tax.Name = "StoreItems";
tax.TaxonName = "Store Items";
tax.Description = "Taxonomy which will classify the items by their type in our online Store";
//
var taxon = manager.CreateTaxon<FlatTaxon>(tag1);
taxon.Title = "Books";
taxon.Name = "Books";
taxon.Description = "This tag categorizes the Books";
tax.Taxa.Add(taxon);
manager.SaveChanges();
As shown in the code example, There is TaxonomyManager class, which is the manager class for Taxonomies. It has helper methods for creating Taxonomy - CreateTaxonomy(), managing Taxonomies and creating Taxons as well. In this particular example, we are creating FlatTaxonomy:
var tax = manager.CreateTaxonomy<FlatTaxonomy>();
Going further, the new Taxonomy should have a name, root Taxon name and description. After setting them, we are creating the first Taxon and set its properties as well:
var taxon = manager.CreateTaxon<FlatTaxon>(tag1);
taxon.Title = "Books";
taxon.Name = "Books";
taxon.Description = "This tag categorizes the Books";
Note how the Taxon is added to the Taxonomies Taxa (Taxon collection) - this is done with a Taxonomy class helper method - Add(Taxon taxon):
Finally, SaveChanges() should be called so a transaction with all the changes could be created and run.
Finding Flat Taxonomies and Taxons
Finding Taxonomy:
TaxonomyManager manager = TaxonomyManager.GetManager();
var taxonomy = manager.GetTaxonomies<FlatTaxonomy>().Where(t => t.Name == "StoreItems").Single();
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<FlatTaxon>().Where(t => t.Name == "Book").Single();
Once we have the Taxa, or Taxonomy instance, the content categorization could begin. This will be shown in a separate article though.