Registering Toolbox Widgets During Sitefinity Module Installation

By in
No comments

When developing a Sitefinity Intra-Site Module you’ll likely be creating public widgets that your users will be dropping onto pages. Rather than relying on your users to manually add them to the toolbox, you can register these controls yourself during module installation.

Here is a demo code snippet that shows an example of how you might do this.

public override void Install(SiteInitializer initializer)
{
	#region Install Toolbox Widgets

	// get section from toolbox
	var config = initializer.Context.GetConfig<ToolboxesConfig>();
	var pageControls = config.Toolboxes["PageControls"];
	var section = pageControls
		.Sections
		.Where<ToolboxSection>(e => e.Name == ToolboxesConfig.ContentToolboxSectionName) // Or other section eg: "NavigationControlsSection"
		.FirstOrDefault();

	// add widget to section if it doesn't exist
	if (!section.Tools.Any<ToolboxItem>(e => e.Name == "MY_CONTROL_NAME"))
	{
		var tool = new ToolboxItem(section.Tools)
		{
			Name = "MY_CONTROL_NAME",
			Title = "Title of Control",
			Description = "Description of control",
			ControlType = "~/Virtual/Path/To/Control.ascx",
			ModuleName = "Module_Name_Optional",
			CssClass = "CSS-class-optional"
		};
		section.Tools.Add(tool);
	}
}

View Code Snippet on Gist

Note that you do not need to call any SaveChanges() or SaveSection() method. Because this is running in an installation context, by retrieving the ToolboxConfig through the SiteInitializer object, the Sitefinity installation mechanism will automatically take care of saving all your changes (or roll them back if the installation fails).

The following two tabs change content below.

selaromdotnet

Senior Developer at iD Tech
Josh loves all things Microsoft and Windows, and develops solutions for Web, Desktop and Mobile using the .NET Framework, Azure, UWP and everything else in the Microsoft Stack. His other passion is music, and in his spare time Josh spins and produces electronic music under the name DJ SelArom. His other passion is music, and in his spare time Josh spins and produces electronic music under the name DJ SelArom.