+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / How to programmatically add an MVC control to a page

How to programmatically add an MVC control to a page

3 posts, 0 answered
  1. Bo Chulindra
    Bo Chulindra avatar
    35 posts
    Registered:
    17 Dec 2013
    24 Jun 2014
    Link to this post

    I am able to create a page programmatically.

     I now want to programmatically add an MVC control to that page. How can I do that?

    I found http://www.sitefinity.com/documentation/documentationarticles/add-a-new-user-control-to-a-page but I don't know what the control path would be for my MVC widget.

    Thanks.

  2. Bo Chulindra
    Bo Chulindra avatar
    35 posts
    Registered:
    17 Dec 2013
    24 Jun 2014
    Link to this post

    I think I got it to work. Below is the code I used. controllerName is the value of the ControllerName property which can be found in Settings > Advanced >  Toolboxes > Toolboxes > PageControls > Sections > (Your Section) > Tools > (Your MVC Widget) > Toolbox item parameters > ControllerName. It should also just be the full name of your controller. For example,  "SitefinityWebApp.Mvc.Controllers.HelloWorldController". placeHolder is the name of the placeholder on your page. For my case, it was "Body".:

            public static void AddMvcControlToPage(Guid pageNodeId, string controllerName, string placeHolder)
            {
                var pageManager = PageManager.GetManager();
                var page = pageManager.GetPageNodes().Single(p => p.Id == pageNodeId);

                var editPage = pageManager.EditPage(page.GetPageData().Id);

                if (editPage == null)
                    throw new InvalidOperationException("Unable to edit page with GUID " + pageNodeId);

                var pageControl = pageManager.CreateControl<PageDraftControl>(Guid.NewGuid());
                pageControl.ObjectType = "Telerik.Sitefinity.Mvc.Proxy.MvcControllerProxy";

                pageControl.PlaceHolder = placeHolder;
                pageControl.IsLayoutControl = false;
                pageManager.SetControlDefaultPermissions(pageControl);


                var prop = pageManager.CreateProperty();
                prop.Name = "ControllerName";
                prop.Value = controllerName;
                pageControl.Properties.Add(prop);


                editPage.Controls.Add(pageControl);
                pageManager.PagesLifecycle.CheckIn(editPage);
                pageManager.SaveChanges();

                var bag = new Dictionary<string, string> {{"ContentType", typeof (PageNode).FullName}};
                WorkflowManager.MessageWorkflow(page.Id, typeof(PageNode), null, "Publish", false, bag);
            }

  3. Bo Chulindra
    Bo Chulindra avatar
    35 posts
    Registered:
    17 Dec 2013
    24 Jun 2014
    Link to this post

    To get all the MVC control controller names, I used the following:

            public static IEnumerable<string> GetAllMvcControlControllerNames()
            {
                var mvcControlNames = new List<string>();

                using (var manager = Config.GetManager())
                {
                    var config = manager.GetSection<ToolboxesConfig>();
                    var pageControls = config.Toolboxes["PageControls"];
                    var sections = pageControls.Sections;

                    foreach (var toolboxSection in sections)
                    {
                        foreach (var toolboxItem in toolboxSection.Tools)
                        {
                            if (toolboxItem.Enabled && toolboxItem.ControlType.Equals(typeof(MvcControllerProxy).FullName))
                                mvcControlNames.Add(toolboxItem.Parameters["ControllerName"]);
                        }
                    }

                }

                return mvcControlNames;
            }

3 posts, 0 answered