---
title: Web parts
---

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

## Web part categories

### Creating a web part category

```csharp

// Gets the root category of the web part tree
WebPartCategoryInfo root = WebPartCategoryInfoProvider.GetWebPartCategoryInfoByCodeName("/");

if (root != null)
{
    // Creates a new web part category object
    WebPartCategoryInfo newCategory = new WebPartCategoryInfo();

    // Sets web part category properties
    newCategory.CategoryDisplayName = "New category";
    newCategory.CategoryName = "NewCategory";
    newCategory.CategoryParentID = root.CategoryID;

    // Saves the web part category to the database
    WebPartCategoryInfoProvider.SetWebPartCategoryInfo(newCategory);
}

```

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

### Updating a web part category

```csharp

// Gets the web part category
WebPartCategoryInfo updateCategory = WebPartCategoryInfoProvider.GetWebPartCategoryInfoByCodeName("NewCategory");               
if (updateCategory != null)
{
    // Updates the web part category properties
    updateCategory.CategoryDisplayName = updateCategory.CategoryDisplayName.ToLower();

    // Saves the changes to the database
    WebPartCategoryInfoProvider.SetWebPartCategoryInfo(updateCategory);
}

```

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

### Updating multiple web part categories

```csharp

// Gets all Web part categories whose display name starts with 'New category'
var categories = WebPartCategoryInfoProvider.GetCategories().WhereStartsWith("CategoryDisplayName", "New category");

// Loops through the web part category objects
foreach (WebPartCategoryInfo modifyCategory in categories)
{
    // Updates the properties
    modifyCategory.CategoryDisplayName = modifyCategory.CategoryDisplayName.ToUpper();

    // Saves the changes to the database
    WebPartCategoryInfoProvider.SetWebPartCategoryInfo(modifyCategory);
}

```

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

### Deleting a web part category

```csharp

// Gets the web part category
WebPartCategoryInfo deleteCategory = WebPartCategoryInfoProvider.GetWebPartCategoryInfoByCodeName("NewCategory");

// Deletes the web part category
WebPartCategoryInfoProvider.DeleteCategoryInfo(deleteCategory);

```

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

## Web parts

### Creating a new web part

```csharp

// Gets a parent category for the new web part
WebPartCategoryInfo category = WebPartCategoryInfoProvider.GetWebPartCategoryInfoByCodeName("NewCategory");

if (category != null)
{
    // Creates a new web part object
    WebPartInfo newWebpart = new WebPartInfo();

    // Sets the web part properties
    newWebpart.WebPartDisplayName = "New web part";
    newWebpart.WebPartName = "NewWebpart";
    newWebpart.WebPartDescription = "This is a new web part.";
    newWebpart.WebPartFileName = "FileName";
    newWebpart.WebPartProperties = "<form></form>";
    newWebpart.WebPartCategoryID = category.CategoryID;

    // Saves the web part to the database
    WebPartInfoProvider.SetWebPartInfo(newWebpart);                 
}

```

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

### Updating a web part

```csharp

// Gets the web part
WebPartInfo updateWebpart = WebPartInfoProvider.GetWebPartInfo("NewWebpart");
if (updateWebpart != null)
{
    // Updates the web part properties
    updateWebpart.WebPartDisplayName = updateWebpart.WebPartDisplayName.ToLower();

    // Saves the changes to the database
    WebPartInfoProvider.SetWebPartInfo(updateWebpart);                  
}

```

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

### Updating multiple web parts

```csharp

// Gets all web parts whose name starts with 'NewWebpart'
var webparts = WebPartInfoProvider.GetWebParts().WhereStartsWith("WebPartName", "NewWebpart");

// Loop through individual web parts
foreach (WebPartInfo webPart in webparts)
{                   
    // Updates the web part properties
    webPart.WebPartDisplayName = webPart.WebPartDisplayName.ToUpper();

    // Saves the changes to the database
    WebPartInfoProvider.SetWebPartInfo(webPart);
}

```

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

### Deleting a web part

```csharp

// Gets the web part
WebPartInfo deleteWebpart = WebPartInfoProvider.GetWebPartInfo("NewWebpart");

// Deletes the web part
WebPartInfoProvider.DeleteWebPartInfo(deleteWebpart);

```

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

## Web part layouts

### Creating a new layout for a web part

```csharp

// Gets the web part
WebPartInfo webpart = WebPartInfoProvider.GetWebPartInfo("NewWebpart");
if (webpart != null)
{
    // Creates a new web part layout object
    WebPartLayoutInfo newLayout = new WebPartLayoutInfo();

    // Sets the web part layout properties
    newLayout.WebPartLayoutDisplayName = "New layout";
    newLayout.WebPartLayoutCodeName = "NewLayout";
    newLayout.WebPartLayoutWebPartID = webpart.WebPartID;
    newLayout.WebPartLayoutCode = "This is the new layout of the NewWebpart web part.";

    // Saves the web part layout to the database
    WebPartLayoutInfoProvider.SetWebPartLayoutInfo(newLayout);
}

```

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

### Updating a web part layout

```csharp

// Gets the web part layout
WebPartLayoutInfo updateLayout = WebPartLayoutInfoProvider.GetWebPartLayoutInfo("NewWebpart", "NewLayout");
if (updateLayout != null)
{
    // Updates the web part layout properties
    updateLayout.WebPartLayoutDisplayName = updateLayout.WebPartLayoutDisplayName.ToLower();

    // Saves the changes to the database
    WebPartLayoutInfoProvider.SetWebPartLayoutInfo(updateLayout);
}

```

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

### Updating multiple web part layouts

```csharp

// Gets all web part layouts whose code name starts with 'NewLayout'
var layouts = WebPartLayoutInfoProvider.GetWebPartLayouts().WhereStartsWith("WebPartLayoutCodeName", "NewLayout");

// Loops through individual web part layouts
foreach (WebPartLayoutInfo layout in layouts)
{                   
    // Updates the web part layout properties
    layout.WebPartLayoutDisplayName = layout.WebPartLayoutDisplayName.ToUpper();

    // Saves the changes to the database
    WebPartLayoutInfoProvider.SetWebPartLayoutInfo(layout);
}

```

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

### Deleting a web part layout

```csharp

// Gets the web part layout
WebPartLayoutInfo deleteLayout = WebPartLayoutInfoProvider.GetWebPartLayoutInfo("NewWebpart", "NewLayout");

// Deletes the web part layout
WebPartLayoutInfoProvider.DeleteWebPartLayoutInfo(deleteLayout);

```

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

## Web part containers

### Creating a new web part container

```csharp

// Creates a new web part container object
WebPartContainerInfo newContainer = new WebPartContainerInfo();

// Sets the container's properties
newContainer.ContainerDisplayName = "New container";
newContainer.ContainerName = "NewContainer";
newContainer.ContainerTextBefore = "<div class=\"NewContainer\">";
newContainer.ContainerTextAfter = "</div>";

// Saves the web part container to the database
WebPartContainerInfoProvider.SetWebPartContainerInfo(newContainer);

```

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

### Updating a web part container

```csharp

// Gets the web part container
WebPartContainerInfo updateContainer = WebPartContainerInfoProvider.GetWebPartContainerInfo("NewContainer");
if (updateContainer != null)
{
    // Updates the container's properties
    updateContainer.ContainerDisplayName = updateContainer.ContainerDisplayName.ToLower();

    // Saves the changes to the database
    WebPartContainerInfoProvider.SetWebPartContainerInfo(updateContainer);
}

```

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

### Updating multiple web part containers

```csharp

// Get all web part containers whose name starts with 'NewContainer'
var containers = WebPartContainerInfoProvider.GetContainers().WhereStartsWith("ContainerName", "NewContainer");

// Loops through individual web part containers
foreach (WebPartContainerInfo container in containers)
{                   
    // Updates the container properties
    container.ContainerDisplayName = container.ContainerDisplayName.ToUpper();

    // Saves the changes to the database
    WebPartContainerInfoProvider.SetWebPartContainerInfo(container);
}

```

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

### Deleting a web part container

```csharp

// Gets the web part container
WebPartContainerInfo deleteContainer = WebPartContainerInfoProvider.GetWebPartContainerInfo("NewContainer");

// Deletes the web part container
WebPartContainerInfoProvider.DeleteWebPartContainerInfo(deleteContainer);

```

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

### Assigning a web part container to a site

```csharp

// Gets the web part container
WebPartContainerInfo container = WebPartContainerInfoProvider.GetWebPartContainerInfo("NewContainer");
if (container != null)
{
    int containerId = container.ContainerID;
    int siteId = SiteContext.CurrentSiteID;

    // Saves the site binding to the database
    WebPartContainerSiteInfoProvider.AddContainerToSite(containerId, siteId);
}

```

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

### Removing a web part container from a site

```csharp

// Gets the web part container
WebPartContainerInfo removeContainer = WebPartContainerInfoProvider.GetWebPartContainerInfo("NewContainer");
if (removeContainer != null)
{
    int siteId = SiteContext.CurrentSiteID;

    // Deletes the site binding from the database
    WebPartContainerSiteInfoProvider.RemoveContainerFromSite(removeContainer.ContainerID, siteId);
}

```

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