---
title: Page templates
---

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

## Page template categories

### Creating a page template category

```csharp

// Creates a new page template category object
PageTemplateCategoryInfo newCategory = new PageTemplateCategoryInfo();

// Sets the category properties
newCategory.DisplayName = "New category";
newCategory.CategoryName = "NewCategory";               

// Saves the page template category to the database
PageTemplateCategoryInfoProvider.SetPageTemplateCategoryInfo(newCategory);

```

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

### Updating a page template category

```csharp

// Gets the page template category
PageTemplateCategoryInfo updateCategory = PageTemplateCategoryInfoProvider.GetPageTemplateCategoryInfo("NewCategory");
if (updateCategory != null)
{
    // Updates the category properties
    updateCategory.DisplayName = updateCategory.DisplayName.ToLower();

    // Saves the changes to the database
    PageTemplateCategoryInfoProvider.SetPageTemplateCategoryInfo(updateCategory);
}

```

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

### Updating multiple page template categories

```csharp

// Gets all page template categories from the first level of the tree
var categories = PageTemplateCategoryInfoProvider.GetPageTemplateCategories().WhereEquals("CategoryLevel", 1);

// Loops through the page template categories
foreach (PageTemplateCategoryInfo category in categories)
{
    // Updates the category properties
    category.DisplayName = category.DisplayName.ToUpper();

    // Saves the changes to the database
    PageTemplateCategoryInfoProvider.SetPageTemplateCategoryInfo(category);
}

```

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

### Deleting a page template category

```csharp

// Gets the page template category
PageTemplateCategoryInfo deleteCategory = PageTemplateCategoryInfoProvider.GetPageTemplateCategoryInfo("NewCategory");

if (deleteCategory != null)
{
    // Deletes the page template category
    PageTemplateCategoryInfoProvider.DeletePageTemplateCategory(deleteCategory);
}

```

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

## Page templates

### Creating a page template

```csharp

// Gets a parent category for the new page template
PageTemplateCategoryInfo category = PageTemplateCategoryInfoProvider.GetPageTemplateCategoryInfo("NewCategory");
if (category != null)
{
    // Creates a new page template object
    PageTemplateInfo newTemplate = new PageTemplateInfo();

    // Sets the page template properties
    newTemplate.DisplayName = "New template";
    newTemplate.CodeName = "NewTemplate";
    newTemplate.Description = "This is a page template description.";
    newTemplate.CategoryID = category.CategoryId;
    newTemplate.PageTemplateSiteID = SiteContext.CurrentSiteID;                                     
    newTemplate.PageTemplateType = PageTemplateTypeEnum.Portal;
    newTemplate.ShowAsMasterTemplate = false;
    newTemplate.IsReusable = true;
    newTemplate.InheritPageLevels = ""; // Configures page nesting for all ancestor pages

    // Saves the page template to the database
    PageTemplateInfoProvider.SetPageTemplateInfo(newTemplate);
}

```

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

### Updating a page template

```csharp

// Gets the page template
PageTemplateInfo updateTemplate = PageTemplateInfoProvider.GetPageTemplateInfo("NewTemplate");
if (updateTemplate != null)
{
    // Updates the page template properties
    updateTemplate.DisplayName = updateTemplate.DisplayName.ToLower();

    // Saves the changes
    PageTemplateInfoProvider.SetPageTemplateInfo(updateTemplate);
}

```

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

### Updating multiple page templates

```csharp

// Gets all page templates assigned to the current site whose code name starts with 'NewTemplate'
var templates = PageTemplateInfoProvider.GetTemplates()
                                            .WhereStartsWith("PageTemplateCodeName", "NewTemplate")
                                            .OnSite(SiteContext.CurrentSiteID);

// Loops through individual page templates
foreach (PageTemplateInfo template in templates)
{                   
    // Updates the template properties
    template.DisplayName = template.DisplayName.ToUpper();

    // Saves the changes to the database
    PageTemplateInfoProvider.SetPageTemplateInfo(template);
}

```

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

### Assigning a page template to a site

```csharp

// Gets the page template
PageTemplateInfo template = PageTemplateInfoProvider.GetPageTemplateInfo("NewTemplate");
if (template != null)
{
    // Assigns the template to the current site
    PageTemplateSiteInfoProvider.AddPageTemplateToSite(template.PageTemplateId, SiteContext.CurrentSiteID);
}

```

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

### Removing a page template from a site

```csharp

// Gets the page template
PageTemplateInfo removeTemplate = PageTemplateInfoProvider.GetPageTemplateInfo("NewTemplate");
if (removeTemplate != null)
{
    // Gets the binding object representing the relationship between the template and the current site
    PageTemplateSiteInfo templateSite = PageTemplateSiteInfoProvider.GetPageTemplateSiteInfo(removeTemplate.PageTemplateId, SiteContext.CurrentSiteID);

    if (templateSite != null)
    {
        // Removes the template from the current site
        PageTemplateSiteInfoProvider.DeletePageTemplateSiteInfo(templateSite);
    }
}

```

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

### Deleting a page template

```csharp

// Gets the page template
PageTemplateInfo deleteTemplate = PageTemplateInfoProvider.GetPageTemplateInfo("NewTemplate");

if (deleteTemplate != null)
{
    // Deletes the page template
    PageTemplateInfoProvider.DeletePageTemplate(deleteTemplate);
}

```

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

## Page template scopes

### Defining a scope for a page template

```csharp

// Gets a page template
PageTemplateInfo template = PageTemplateInfoProvider.GetPageTemplateInfo("NewTemplate");

// Checks whether the page template exists
if (template != null)
{
    // Configures the page template to be usable only within certain scopes (not all pages)
    template.PageTemplateForAllPages = false;

    // Creates a new template scope object
    PageTemplateScopeInfo newScope = new PageTemplateScopeInfo();

    // Assigns the scope to the template and sets the scope properties
    newScope.PageTemplateScopeTemplateID = template.PageTemplateId;
    newScope.PageTemplateScopeSiteID = SiteContext.CurrentSiteID;
    newScope.PageTemplateScopePath = "/";
    newScope.PageTemplateScopeLevels = "/{0}/{1}";

    // Saves the template scope to the database
    PageTemplateScopeInfoProvider.SetPageTemplateScopeInfo(newScope);

    // Saves the page template changes to the database
    PageTemplateInfoProvider.SetPageTemplateInfo(template);
}

```

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

### Deleting page template scopes

```csharp

// Gets a page template
PageTemplateInfo template = PageTemplateInfoProvider.GetPageTemplateInfo("NewTemplate");

// Checks whether the page template exists
if (template != null)
{
    // Gets all scopes used by the given page template
    var scopes = PageTemplateScopeInfoProvider.GetTemplateScopes().WhereEquals("PageTemplateScopeTemplateID", template.PageTemplateId);

    foreach (PageTemplateScopeInfo deleteScope in scopes)
    {
        // Deletes the page template scope
        PageTemplateScopeInfoProvider.DeletePageTemplateScopeInfo(deleteScope);
    }

    // Configures the page template to be usable on all pages
    template.PageTemplateForAllPages = true;

    // Saves the page template changes to the database
    PageTemplateInfoProvider.SetPageTemplateInfo(template);
}

```

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