---
title: Tags
---

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

## Tag groups

### Creating a tag group

```csharp

// Creates a new tag group object
TagGroupInfo newGroup = new TagGroupInfo();

// Sets the tag group properties
newGroup.TagGroupDisplayName = "New tag group";
newGroup.TagGroupName = "NewTagGroup";
newGroup.TagGroupDescription = "This tag group was created through the API.";
newGroup.TagGroupSiteID = SiteContext.CurrentSiteID;
newGroup.TagGroupIsAdHoc = false;

// Saves the new tag group to the database
TagGroupInfo.Provider.Set(newGroup);

```

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

### Updating a tag group

```csharp

// Gets the tag group
TagGroupInfo updateGroup = TagGroupInfo.Provider.Get("NewTagGroup", SiteContext.CurrentSiteID);

if (updateGroup != null)
{
    // Updates the tag group properties
    updateGroup.TagGroupDisplayName = updateGroup.TagGroupDisplayName.ToLower();

    // Saves the updated tag group to the database
    TagGroupInfo.Provider.Set(updateGroup);
}

```

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

### Updating multiple tag groups

```csharp

// Gets all tag groups defined for the current site whose name starts with 'New'
var tagGroups = TagGroupInfo.Provider.Get()
                                    .WhereEquals("TagGroupSiteID", SiteContext.CurrentSiteID)
                                    .WhereStartsWith("TagGroupName", "New");

// Loops through individual tag groups
foreach (TagGroupInfo tagGroup in tagGroups)
{
    // Updates the tag group properties
    tagGroup.TagGroupDisplayName = tagGroup.TagGroupDisplayName.ToUpper();

    // Saves the updated tag group to the database
    TagGroupInfo.Provider.Set(tagGroup);
}

```

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

### Deleting a tag group

```csharp

// Gets the tag group
TagGroupInfo deleteGroup = TagGroupInfo.Provider.Get("NewTagGroup", SiteContext.CurrentSiteID);

if (deleteGroup != null)
{
    // Deletes the tag group
    TagGroupInfo.Provider.Delete(deleteGroup);
}

```

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

## Page tags

### Adding tags to a page

```csharp

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

// Gets a tag group
TagGroupInfo tagGroup = TagGroupInfo.Provider.Get("NewTagGroup", SiteContext.CurrentSiteID);

if ((page != null) && (tagGroup != null))
{
    // Assigns the tag group to the page
    page.DocumentTagGroupID = tagGroup.TagGroupID;

    // Adds the "API test" and "Examples" tags to the page
    page.DocumentTags = "\"API test\", Examples";

    // Updates the page in the database
    page.Update();
}

```

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

### Removing tags from a page

```csharp

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

if (page != null)
{
    // Checks that the page has tag content
    if (!String.IsNullOrEmpty(page.DocumentTags))
    {
        // Removes all tags from the page
        page.DocumentTags = "";

        // Updates the page in the database
        page.Update();
    }
}

```

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