Register a new widget in Sitefinity CMS toolbox
To use newly created widgets on a Sitefinity CMS page, you must register them in the toolbox. Once you do this, the widgets are available for drag-and-drop in the page editor user interface.
There are several ways you can register a widget. All of them require a ControlType parameter. The value of this parameter depends on the type of widget used.
- When registering a custom widget , built into an assembly, the parameter is the fully qualified type name of the widget.
- When registering a user widget , the parameter is the virtual URL of the user widget.
Register a widget in Sitefinity CMS backend
You can register a widget through Sitefinity’s advanced settings UI or through Sitefinity’s configuration file. For more information, see Register a new widget in the backend.
Register a widget through code using the configuration manager
You may want to register your custom widget in the toolbox through the API, if, for example, the widget is part of a custom module and must be registered when the module is installed.
To register a widget with Sitefinity CMS configuration manager, use the following code:
using System.Linq;
using Telerik.Sitefinity.Configuration;
using Telerik.Sitefinity.Modules.Pages;
using Telerik.Sitefinity.Modules.Pages.Configuration;
namespace SitefinityWebApp
{
public class RegisterWidgetInToolbox
{
public static void RegisterWidget()
{
var configManager = ConfigManager.GetManager();
var config = configManager.GetSection<ToolboxesConfig>();
var controls = config.Toolboxes["PageControls"];
var sectionName = "MySection";
var section = controls.Sections.Where<ToolboxSection>(e => e.Name == sectionName).FirstOrDefault();
if (section == null)
{
section = new ToolboxSection(controls.Sections)
{
Name = sectionName,
Title = sectionName,
Description = sectionName,
ResourceClassId = typeof(PageResources).Name
};
controls.Sections.Add(section);
}
var controlName = "MyControl";
var controlType = typeof(MyUserWidget);
if (!section.Tools.Any<ToolboxItem>(e => e.Name == controlName))
{
var tool = new ToolboxItem(section.Tools)
{
Name = controlName,
Title = controlName,
Description = controlName,
ControlType = controlType.AssemblyQualifiedName
};
section.Tools.Add(tool);
}
configManager.SaveSection(config);
}
}
}
In the snippet above, to reflect your custom widget values, you must change the name of the section, the name of the widget, and the type of the widget.