More in this section

Forums / General Discussions / Registering custom control in Toolbox

Registering custom control in Toolbox

8 posts, 0 answered
  1. higgsy
    higgsy avatar
    336 posts
    Registered:
    05 Aug 2010
    28 Jan 2011
    Link to this post
    Hi,

    Apologies if this has already been asked, I couldn't find any threads that were quite relevant enough.

    In my project I have a number of custom controls, I also have an external project referenced which has a number of custom controls. I want some of the controls to appear in the toolbox, therefore I wanted to register a number of toolbox controls from Application_Start.

    Can this be done? I think I need to use the SiteInitializer class, but I'm not sure how?

    Thanks
    higgsy
  2. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    02 Feb 2011
    Link to this post
    Hello higgsy,

    If you are not using a module, but just want to register controls in the toolbox you can create a Global.asax file in the root of the Site. Then subscribe to ApplicationStart of SystemManager:
    protected void Application_Start(object sender, EventArgs e)
    {
        SystemManager.ApplicationStart +=new EventHandler<EventArgs>(SystemManager_ApplicationStart);
    }
     
    void  SystemManager_ApplicationStart(object sender, EventArgs e)
    {
        //add code to regsiter controls here
    }

    You can find code which registers custom controls in the toolbox in the custom module samples of the SDK (Products, Jobs, JobsIntermediate).

    Kind regards,
    Radoslav Georgiev
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. higgsy
    higgsy avatar
    336 posts
    Registered:
    05 Aug 2010
    02 Feb 2011
    Link to this post
    Hi Radoslav,

    Thanks for the response.

    Could you be a little mopre specific about the code that will work from the global.asax file - I have seen code in the modules such as SiteInitializer, but nothing seems to happen. If you could give me an idea of the functional or class i should be calling it would be most helpful.

    Thanks
    higgsy
  4. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    05 Feb 2011
    Link to this post
    Hello higgsy,

    You should look at the install configuration method:
    /// <summary>
    /// Installs module's toolbox configuration.
    /// </summary>
    /// <param name="initializer">The initializer.</param>
    protected override void InstallConfiguration(SiteInitializer initializer)
    {
        var config = initializer.Context.GetConfig<ToolboxesConfig>();
        var pageControls = config.Toolboxes["PageControls"];
        var section = pageControls
            .Sections
            .Where<ToolboxSection>(e => e.Name == ToolboxesConfig.ContentToolboxSectionName)
            .FirstOrDefault();
     
        section.Tools.Add(new ToolboxItem(section.Tools)
        {
            Name = "JobApplicationUpload",
            Title = "JobApplicationsUploadTitle",
            Description = "JobApplicationsUploadDescription",
            ResourceClassId = typeof(JobsResources).Name,
            ControlType = typeof(JobApplicationUpload).AssemblyQualifiedName
        });
    }

    Instead of using SiteInitializer to get the toolboxes config you can use the approach from this help article: Working with Configurations Programmatically.

    Greetings,
    Radoslav Georgiev
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  5. higgsy
    higgsy avatar
    336 posts
    Registered:
    05 Aug 2010
    17 Feb 2011
    Link to this post
    Hi Radoslav,

    I have tried to register a toolbox control using the following code:

    var config = Telerik.Sitefinity.Configuration.Config.Get<ToolboxesConfig>();
    var pageControls = config.Toolboxes["PageControls"];
    var section = pageControls
        .Sections
        .Where<ToolboxSection>(t => t.Name == ToolboxesConfig.ContentToolboxSectionName)
        .FirstOrDefault();
     
    section.Tools.Add(new ToolboxItem(section.Tools) {
        Name = "test",
        Title = "test",
        Description = "test",
        ControlType = "~/lib/controls/ui-elements/FrontEnd/ImageSlider.ascx"
    });

    When I run the code it doesnt update the toolboxesconfig.config file, however if I try to run it for a second time I get the error message: An item with the same key has already been added.

    How can this be possible? Is my code wrong or is it being stored somewhere less obvious?

    Thanks
    higgsy
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    18 Feb 2011
    Link to this post
    Hello higgsy,

    The code looks fine. Can you try to restart the application and see whether the widget is added to Administration >> Settings >> Advanced >> Toolboxes section.
    You can also try to extend the code as below to avoid the error which says that there is an entry


    if (!section.Tools.Any<ToolboxItem>(e => e.Name == "test"))
                {
                    var tool = new ToolboxItem(section.Tools)
                    {
                        Name = "test",
                        Title = "test",
                        Description = "test",
                        ResourceClassId = classId,
                        ModuleName = ContentModule.ModuleName,
                        ControlType = "";
                    };
                    section.Tools.Add(tool);
                }

    Best wishes,
    Ivan Dimitrov
    the Telerik team
  7. higgsy
    higgsy avatar
    336 posts
    Registered:
    05 Aug 2010
    18 Feb 2011
    Link to this post
    Hi Ivan,

    I have restarted the application and the item does not appear in the toolbox, I have cheked the toolboxesconfig.config file aswell.

    I can run the code again now and I dont get the error, however if I then run it again i do get the error. This suggests to me the data isnt being persisted somehow.

    I dont understand how the code can produce an error that an item with the same key has already been added if its not checking against the toolboxesconfig.config file.

    Is it possible that this data is getting written to the database but not to the toolboxesconfig.config file?

    Thanks
    higgsy
  8. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    22 Feb 2011
    Link to this post
    Hi higgsy,

    You should use instance of ConfigManager and call SaveSection method

    ConfigManager manager = Config.GetManager();
    var config = manager.GetSection<ToolboxesConfig>();
    var pageControls = config.Toolboxes["PageControls"];
    var section = pageControls
        .Sections
        .Where<ToolboxSection>(tb => tb.Name == ToolboxesConfig.ContentToolboxSectionName)
        .FirstOrDefault();
    if (!section.Tools.Any<ToolboxItem>(av => av.Name == "AvatarWidget"))
    {
        var tool = new ToolboxItem(section.Tools)
        {
            Name = "AvatarWidget",
            Title = "AvatarWidget",
            Description = "AvatarWidget",
            CssClass = "sfBlogsViewIcn",
            ResourceClassId = "",
            ControlType = "~/Controls/AvatarWidget.ascx"
        };
        section.Tools.Add(tool);
        manager.SaveSection(config);
    }

    Best wishes,
    Ivan Dimitrov
    the Telerik team
8 posts, 0 answered