---
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
CategoryInfo.Provider.Set(category);

```

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

### Updating a page category

```csharp

// Gets the page category
CategoryInfo category = CategoryInfo.Provider.Get("Carnivores", SiteContext.CurrentSiteID);

// 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
    CategoryInfo.Provider.Set(category);
}

```

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

### Updating multiple page categories

```csharp

// Gets all first level categories on the current site
var categories = CategoryInfo.Provider.Get().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
    CategoryInfo.Provider.Set(category);
}

```

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

### Creating subcategories

```csharp

// Gets the parent category
CategoryInfo parentCategory = CategoryInfo.Provider.Get("carnivores", SiteContext.CurrentSiteID);

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
    CategoryInfo.Provider.Set(subcategory);
}

```

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

### Adding pages to categories

```csharp

// Gets a page category from the system
CategoryInfo category = CategoryInfo.Provider.Get("Dogs", SiteContext.CurrentSiteID);

if (category != null)
{
    // Gets a page
    TreeNode page = new DocumentQuery<TreeNode>()
                        .Path("/Example", PathTypeEnum.Single)
                        .OnSite("MySite")
                        .Culture("en-us")
                        .TopN(1)
                        .FirstOrDefault();

    // Adds the page to the category
    DocumentCategoryInfo.Provider.Add(page.DocumentID, category.CategoryID);
}

```

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

### Removing pages from categories

```csharp

// Gets a page category from the system
CategoryInfo category = CategoryInfo.Provider.Get("Dogs", SiteContext.CurrentSiteID);

if (category != null)
{
    // Gets a page
    TreeNode page = new DocumentQuery<TreeNode>()
            .Path("/Example", PathTypeEnum.Single)
            .OnSite("MySite")
            .Culture("en-us")
            .TopN(1)
            .FirstOrDefault();

    // Gets the page category relationship record
    DocumentCategoryInfo documentCategory = DocumentCategoryInfo.Provider.Get(page.DocumentID, category.CategoryID);

    if (documentCategory != null)
    {
        // Removes the relationship record from the database
        DocumentCategoryInfo.Provider.Delete(documentCategory);
    }
}

```

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

### Deleting page categories

```csharp

// Gets the page category
CategoryInfo category = CategoryInfo.Provider.Get("Carnivores", SiteContext.CurrentSiteID);

if (category != null)
{
    // Deletes the category
    CategoryInfo.Provider.Delete(category);
}

```

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