---
title: Categories
---

> 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:

### Creating a new page category

```csharp

// Creates a new category object
CategoryInfo category = new CategoryInfo();

// Sets the category properties
category.CategoryDisplayName = "Carnivores";
category.CategoryName = "Carnivores";
category.CategoryDescription = "Animals that feed on other animals";
category.CategorySiteID = SiteContext.CurrentSiteID;
category.CategoryEnabled = true;

// Saves the category to the database
CategoryInfoProvider.SetCategoryInfo(category);

```

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

### Updating a page category

```csharp

// Gets the page category
CategoryInfo category = CategoryInfoProvider.GetCategoryInfo("Carnivores", SiteContext.CurrentSiteName);

// Checks if the previous method retrieved the record
if (category != null)
{
    // Updates the category properties
    category.CategoryDescription = "People who are not vegetarians";

    // Saves the changes to the database
    CategoryInfoProvider.SetCategoryInfo(category);
}

```

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

### Updating multiple page categories

```csharp

// Gets all first level categories on the current site
var categories = CategoryInfoProvider.GetCategories().WhereEquals("CategoryLevel", 0).OnSite(SiteContext.CurrentSiteID);

// Loops through the categories
foreach (CategoryInfo category in categories)
{
    // Updates the code names of the categories
    category.CategoryName = category.CategoryName.ToLower();

    // Saves the changes to the database
    CategoryInfoProvider.SetCategoryInfo(category);
}

```

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

### Creating subcategories

```csharp

// Gets the parent category
CategoryInfo parentCategory = CategoryInfoProvider.GetCategoryInfo("carnivores", SiteContext.CurrentSiteName);

if (parentCategory != null)
{
    // Creates a new category object
    CategoryInfo subcategory = new CategoryInfo();

    // Sets basic properties
    subcategory.CategoryDisplayName = "Dogs";
    subcategory.CategoryName = "Dogs";
    subcategory.CategoryDescription = "Man's best friends";
    subcategory.CategorySiteID = SiteContext.CurrentSiteID;
    subcategory.CategoryEnabled = true;

    // Assigns to the parent category
    subcategory.CategoryParentID = parentCategory.CategoryID;

    // Saves the category to the database
    CategoryInfoProvider.SetCategoryInfo(subcategory);
}

```

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

### Adding pages to categories

```csharp

// Gets the category
CategoryInfo category = CategoryInfoProvider.GetCategoryInfo("Dogs", SiteContext.CurrentSiteName);

if (category != null)
{
    // Creates a new instance of the tree provider, needed for page operations
    TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);

    // Gets a page
    TreeNode root = tree.SelectNodes()
        .Path("/")
        .OnCurrentSite()
        .CombineWithDefaultCulture()
        .TopN(1)
        .FirstOrDefault();

    // Adds the page to the category
    DocumentCategoryInfoProvider.AddDocumentToCategory(root.DocumentID, category.CategoryID);
}

```

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

### Removing pages from categories

```csharp

// Gets the category
CategoryInfo category = CategoryInfoProvider.GetCategoryInfo("Dogs", SiteContext.CurrentSiteName);

if (category != null)
{
    // Creates a new instance of the tree provider, needed for page operations
    TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);

    // Gets the page
    TreeNode root = tree.SelectNodes()
        .Path("/")
        .OnCurrentSite()
        .CombineWithDefaultCulture()
        .TopN(1)
        .FirstOrDefault();

    // Gets the page category relationship record
    DocumentCategoryInfo documentCategory = DocumentCategoryInfoProvider.GetDocumentCategoryInfo(root.DocumentID, category.CategoryID);

    if (documentCategory != null)
    {
        // Removes the relationship record from the database
        DocumentCategoryInfoProvider.DeleteDocumentCategoryInfo(documentCategory);
    }
}

```

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

### Deleting page categories

```csharp

// Gets the page category
CategoryInfo category = CategoryInfoProvider.GetCategoryInfo("Carnivores", SiteContext.CurrentSiteName);

if (category != null)
{
    // Deletes the category
    CategoryInfoProvider.DeleteCategoryInfo(category);
}

```

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