Skip to content

Instantly share code, notes, and snippets.

@jonathanread
Created March 13, 2018 20:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonathanread/11a428bbb1303aa5736c0d451c391376 to your computer and use it in GitHub Desktop.
Save jonathanread/11a428bbb1303aa5736c0d451c391376 to your computer and use it in GitHub Desktop.
This updated a specific Toolbox section and tool to 'disable' them. Specifically created to work around issue with UI not updating custom content type tool items
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Telerik.Sitefinity.Configuration;
using Telerik.Sitefinity.Modules.Pages.Configuration;
namespace SitefinityWebApp.Helpers
{
public static class ToolboxConfigHelper
{
public static void ModifyToolBox(string sectionNameToCopyFrom, List<string> toolsToSkip = null)
{
var configManager = ConfigManager.GetManager();
using (new Telerik.Sitefinity.Data.ElevatedModeRegion(configManager))
{
var config = configManager.GetSection<ToolboxesConfig>();
var controls = config.Toolboxes["PageControls"];
ToolboxSection section = controls.Sections.Where<ToolboxSection>(e => e.Name == sectionNameToCopyFrom).FirstOrDefault();
if (section != null)
{
foreach (var tool in section.Tools)
{
if (toolsToSkip.Contains(tool.Name))
{
tool.Enabled = false;
}
}
configManager.SaveSection(config);
}
}
}
private static ToolboxSection CreateNewSection(Toolbox controls, SectionData newSectionConfig)
{
ToolboxSection section = new ToolboxSection(controls.Sections)
{
Name = newSectionConfig.Name,
Title = newSectionConfig.Title,
Description = newSectionConfig.Description,
ResourceClassId = null
};
controls.Sections.Add(section);
return section;
}
}
public class SectionData
{
public string Name { get; set; }
public string Title { get; set; }
public string Description { get; set; }
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UpdateToolbox.aspx.cs" Inherits="SitefinityWebApp.Helpers.Test" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label runat="server" AssociatedControlID="ToolboxSectionTextBox" Text="Toolbox Section Name to Modify"></asp:Label><asp:TextBox runat="server" ID="ToolboxSectionTextBox"></asp:TextBox>
</div>
<div>
<asp:Label runat="server" AssociatedControlID="ControlsToDisableTextBox" Text="Comma list of Tools to disable by name"></asp:Label><asp:TextBox runat="server" ID="ControlsToDisableTextBox" ToolTip="Comma Seperated like a tool name(s) to disable"></asp:TextBox>
</div>
<asp:Button runat="server" ID="UpdateConfigButton" Text="Update Toolbox Config" OnClick="UpdateConfigButton_Click" />
<asp:Label runat="server" ID="MsgLabel"></asp:Label>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Sitefinity.Data;
using Telerik.Sitefinity.DynamicModules;
using Telerik.Sitefinity.DynamicModules.Model;
using Telerik.Sitefinity.Model;
using Telerik.Sitefinity.Utilities.TypeConverters;
namespace SitefinityWebApp.Helpers
{
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void UpdateConfigButton_Click(object sender, EventArgs e)
{
if(ToolboxSectionTextBox.Text.IsNullOrWhitespace() || ControlsToDisableTextBox.Text.IsNullOrWhitespace())
{
MsgLabel.Text = "Include both fields";
return;
}
try
{
List<string> disableTools = ControlsToDisableTextBox.Text.Split(',').Select(i => i.Trim()).ToList();
ToolboxConfigHelper.ModifyToolBox(ToolboxSectionTextBox.Text, disableTools);
MsgLabel.Text = "Success!";
}
catch (Exception ex)
{
MsgLabel.Text = ex.Message;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment