---
title: Taxonomies
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

List of examples:

## Dependency injection

### Initialize required services

```csharp
// Initializes all services and provider classes used within
// the API examples on this page using dependency injection.

private readonly ITaxonomyManager taxonomyManager;
private readonly IInfoProvider<TaxonomyInfo> taxonomyInfoProvider;
private readonly IInfoProvider<TagInfo> tagInfoProvider;
private readonly IInfoProvider<ContentLanguageInfo> contentLanguageInfoProvider;

public TaxonomiesServices(ITaxonomyManager taxonomyManager,
                          IInfoProvider<TaxonomyInfo> taxonomyInfoProvider,
                          IInfoProvider<TagInfo> tagInfoProvider,
                          IInfoProvider<ContentLanguageInfo> contentLanguageInfoProvider)

{
    this.taxonomyManager = taxonomyManager;
    this.taxonomyInfoProvider = taxonomyInfoProvider;
    this.tagInfoProvider = tagInfoProvider;
    this.contentLanguageInfoProvider = contentLanguageInfoProvider;
}
```

[> Back to list of examples](#toc)

## Taxonomies

### Create taxonomies

```csharp
// Creates a new taxonomy object with the provided values
var taxonomy = new TaxonomyInfo
{
    // Code name
    TaxonomyName = "ChocolateTypes",
    // Display name in the default language
    TaxonomyTitle = "Chocolate types",
    // Taxonomy description
    TaxonomyDescription = "Types of chocolate"
};

// Saves the taxonomy to the database
taxonomyInfoProvider.Set(taxonomy);
```

[> Back to list of examples](#toc)

### Create tags

```csharp
// Tags in a taxonomy are keywords that can be assigned to pieces of content

// Gets the taxonomy to which the tag will be added
TaxonomyInfo chocolateTaxonomyInfo = taxonomyInfoProvider.Get("ChocolateTypes");

// Creates a new tag in the Chocolate types taxonomy
var milkTagInfo = new TagInfo
{
    // Code name
    TagName = "Milk",

    // Display name in the default language
    TagTitle = "Milk",

    // ID of the taxonomy to which the tag belongs
    TagTaxonomyID = chocolateTaxonomyInfo.TaxonomyID,

    // Tag description in the default language
    TagDescription = "Milk chocolate",

    // Order of the tag among the tags on the same level when displayed in the UI
    TagOrder = 1,
};

// Saves the tag to the database
tagInfoProvider.Set(milkTagInfo);
```

[> Back to list of examples](#toc)

### Create tag hierarchy

```csharp
// Tags can be organized into a hierarchy using parent-child relationships
// This example demonstrates how to create a parent-child relationship
// between existing tags

// Gets the child tag
TagInfo caramelTagInfo = tagInfoProvider.Get("Caramel");

// Gets the parent tag
TagInfo milkTagInfo = tagInfoProvider.Get("Milk");

// Assigns 'Milk' as the parent tag of 'Caramel'
caramelTagInfo.TagParentID = milkTagInfo.TagID;

// Saves the updated tag
caramelTagInfo.Update();
```

[> Back to list of examples](#toc)

### Translate taxonomies

```csharp
// Translations of the taxonomy title are stored in the 'TaxonomyMetadata' column
// To ensure the correct format, serialize the 'TaxonomyMetadata' object via its 'Serialize' method before saving
// it to the database

// This example assumes that no metadata exists yet for the taxonomy
// To work with existing metadata, deserialize it first using the 'TaxonomyMetadata.Deserialize' static method

// The GUID of the language to translate to
Guid spanishGuid = contentLanguageInfoProvider.Get("es").ContentLanguageGUID;

// Creates a new taxonomy translation
TaxonomyTranslation spanishTranslation = new TaxonomyTranslation
{
    Title = "Tipos de chocolate"
};

// Creates a dictionary with the Spanish translation stored under the GUID of the Spanish language
// Other languages can be added to the dictionary using the same pattern
Dictionary<Guid, TaxonomyTranslation> translations = new()
{
    { spanishGuid, spanishTranslation }
};

// Creates new taxonomy metadata and assigns the 'translations' dictionary to its Translations property
var metadata = new TaxonomyMetadata { Translations = translations };

// Gets the taxonomy object to translate
TaxonomyInfo chocolateInfo = taxonomyInfoProvider.Get("ChocolateTypes");

// Serializes the metadata object and sets it as the metadata for the 'Chocolate types' taxonomy
chocolateInfo.TaxonomyMetadata = metadata.Serialize();

// Saves the changes to the database
chocolateInfo.Update();
```

[> Back to list of examples](#toc)

### Translate tags

```csharp
// The translations of the tag titles and descriptions are stored in the 'TagMetadata' column
// To ensure the correct format, serialize the 'TagMetadata' object using its 'Serialize' method
// before saving it to the database

// This example assumes that no metadata exists yet for the tag
// To work with existing metadata, deserialize it first using the 'TagMetadata.Deserialize' static method


// The GUID of the language to translate to
Guid spanishGuid = contentLanguageInfoProvider.Get("es").ContentLanguageGUID;

// Creates a new tag translation
TagTranslation spanishTranslation = new TagTranslation
{
    Title = "Con leche",
    Description = "Chocolate con leche"
};

// Creates a dictionary with the Spanish translation stored under the GUID of the Spanish language
Dictionary<Guid, TagTranslation> translations = new()
{
    { spanishGuid, spanishTranslation }
};

// Creates new tag metadata and assigns the 'translations' dictionary to its Translations property
var metadata = new TagMetadata { Translations = translations };

// Gets the tag object to translate
TagInfo tagInfo = tagInfoProvider.Get("Milk");

// Serializes the metadata and sets it as the metadata for the 'Milk' tag
tagInfo.TagMetadata = metadata.Serialize();

// Saves the changes to the database
tagInfo.Update();
```

[> Back to list of examples](#toc)

### Move tags

```csharp
// Tags can be organized in a hierarchy using parent-child relationships
// This example demonstrates how to move a tag under a new parent tag

// Gets the ID of the tag to move
int tagId = tagInfoProvider.Get("Caramel").TagID;

// Gets the ID of the new parent tag
// Use '0' value to move the tag to the root level in the hierarchy
int newParentTagId = tagInfoProvider.Get("Dark").TagID;

// Moves the tag to the new parent
await taxonomyManager.MoveTag
(
    tagId,
    newParentTagId,
    // Display order
    order: 1
);
```

[> Back to list of examples](#toc)

### Delete tags

```csharp
//  Gets the tag object to be deleted
var tagInfo = tagInfoProvider.Get("Milk");

// Permanently deletes the tag and all its child tags from the database
await taxonomyManager.DeleteTag(tagInfo.TagID);
```

[> Back to list of examples](#toc)

### Delete taxonomies

```csharp
// Gets the taxonomy object to be deleted
TaxonomyInfo taxonomyInfo = taxonomyInfoProvider.Get("ChocolateTypes");

// Permanently deletes the taxonomy from the database
if (await taxonomyManager.DeleteTaxonomy(taxonomyInfo.TaxonomyID))
{
    Console.WriteLine("Taxonomy successfully deleted.");
}
else
    throw new Exception("The taxonomy could not be deleted because it still contains tags.");
```

[> Back to list of examples](#toc)
