---
title: Widgets
---

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

## Widget categories

### Creating a new widget category

```csharp

// Creates a new widget category object
WidgetCategoryInfo newCategory = new WidgetCategoryInfo();

// Sets the widget category properties
newCategory.WidgetCategoryDisplayName = "New category";
newCategory.WidgetCategoryName = "NewCategory";

// Saves the widget category to the database
WidgetCategoryInfoProvider.SetWidgetCategoryInfo(newCategory);

```

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

### Updating a widget category

```csharp

// Gets the widget category
WidgetCategoryInfo updateCategory = WidgetCategoryInfoProvider.GetWidgetCategoryInfo("NewCategory");
if (updateCategory != null)
{
    // Updates the widget category properties
    updateCategory.WidgetCategoryDisplayName = updateCategory.WidgetCategoryDisplayName.ToLower();

    // Saves the changes to the database
    WidgetCategoryInfoProvider.SetWidgetCategoryInfo(updateCategory);
}

```

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

### Updating multiple widget categories

```csharp

// Gets all widget categories whose name starts with 'NewCategory'
var categories = WidgetCategoryInfoProvider.GetWidgetCategories().WhereStartsWith("WidgetCategoryName", "NewCategory");

// Loops through individual widget categories
foreach (WidgetCategoryInfo category in categories)
{
    // Updates the category properties
    category.WidgetCategoryDisplayName = category.WidgetCategoryDisplayName.ToUpper();

    // Saves the changes to the database
    WidgetCategoryInfoProvider.SetWidgetCategoryInfo(category);
}

```

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

### Deleting a widget category

```csharp

// Gets the widget category
WidgetCategoryInfo deleteCategory = WidgetCategoryInfoProvider.GetWidgetCategoryInfo("NewCategory");

if (deleteCategory != null)
{
    // Deletes the widget category
    WidgetCategoryInfoProvider.DeleteWidgetCategoryInfo(deleteCategory);                    
}

```

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

## Widgets

### Creating a new widget

```csharp

// Gets a parent web part and category for the widget
WebPartInfo webpart = WebPartInfoProvider.GetWebPartInfo("AbuseReport");
WidgetCategoryInfo category = WidgetCategoryInfoProvider.GetWidgetCategoryInfo("NewCategory");

// Verifies that the parent category and web part exist, and that the web part is not inherited
// Widgets cannot be created from inherited web parts
if ((webpart != null) && (webpart.WebPartParentID == 0) && (category != null))
{
    // Creates a new widget object 
    WidgetInfo newWidget = new WidgetInfo();

    // Sets the widget properties
    newWidget.WidgetName = "NewWidget";
    newWidget.WidgetDisplayName = "New widget";
    newWidget.WidgetDescription = webpart.WebPartDescription;

    // Loads the property definitions from the parent web part
    newWidget.WidgetProperties = FormHelper.GetFormFieldsWithDefaultValue(webpart.WebPartProperties, "visible", "false");

    // Assigns the widget under the given category and web part
    newWidget.WidgetWebPartID = webpart.WebPartID;
    newWidget.WidgetCategoryID = category.WidgetCategoryID;

    // Saves the new widget to the database
    WidgetInfoProvider.SetWidgetInfo(newWidget);
}

```

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

### Updating a widget

```csharp

// Gets the widget
WidgetInfo updateWidget = WidgetInfoProvider.GetWidgetInfo("NewWidget");
if (updateWidget != null)
{
    // Updates the widget properties
    updateWidget.WidgetDisplayName = updateWidget.WidgetDisplayName.ToLower();

    // Saves the changes
    WidgetInfoProvider.SetWidgetInfo(updateWidget);
}

```

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

### Updating multiple widgets

```csharp

// Gets all widgets whose name starts with 'NewWidget'
var widgets = WidgetInfoProvider.GetWidgets().WhereStartsWith("WidgetName", "NewWidget");

// Loops through individual widgets
foreach (WidgetInfo widget in widgets)
{
    // Updates the widget properties
    widget.WidgetDisplayName = widget.WidgetDisplayName.ToUpper();

    // Saves the changes to the database
    WidgetInfoProvider.SetWidgetInfo(widget);
}

```

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

### Deleting a widget

```csharp

// Gets the widget
WidgetInfo deleteWidget = WidgetInfoProvider.GetWidgetInfo("NewWidget");

if (deleteWidget != null)
{
    // Deletes the widget
    WidgetInfoProvider.DeleteWidgetInfo(deleteWidget);
}

```

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

## Widget security

### Setting the security options for a widget

```csharp

// Gets the widget
WidgetInfo widget = WidgetInfoProvider.GetWidgetInfo("NewWidget");

// Checks whether the widget exists
if (widget != null)
{
    // Allows the widget to be used in editor widget zones
    widget.WidgetForEditor = true;

    // Configures the widget to be usable only by assigned roles
    widget.AllowedFor = SecurityAccessEnum.AuthorizedRoles;

    // Saves the widget changes to the database
    WidgetInfoProvider.SetWidgetInfo(widget);
}

```

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

### Assigning a role to a widget

```csharp

// Gets role, widget and permission objects
RoleInfo role = RoleInfoProvider.GetRoleInfo("Admin", SiteContext.CurrentSiteID);
WidgetInfo widget = WidgetInfoProvider.GetWidgetInfo("NewWidget");
PermissionNameInfo permission = PermissionNameInfoProvider.GetPermissionNameInfo("AllowedFor", "Widgets", null);

// Checks that all of the objects exist
if ((role != null) && (widget != null) && (permission != null))
{
    // Assigns the role to the widget 
    // Allows members of the role to work with the widget (if the widget is allowed only for authorized roles)
    WidgetRoleInfoProvider.AddRoleToWidget(role.RoleID, widget.WidgetID, permission.PermissionId);
}

```

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

### Removing a role from a widget

```csharp

// Gets role, widget and permission objects
RoleInfo role = RoleInfoProvider.GetRoleInfo("Admin", SiteContext.CurrentSiteID);
WidgetInfo widget = WidgetInfoProvider.GetWidgetInfo("NewWidget");
PermissionNameInfo permission = PermissionNameInfoProvider.GetPermissionNameInfo("AllowedFor", "Widgets", null);

// Checks that all of the objects exist
if ((role != null) && (widget != null) && (permission != null))
{
    // Removes the role from the widget
    WidgetRoleInfoProvider.RemoveRoleFromWidget(role.RoleID, widget.WidgetID, permission.PermissionId);
}

```

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