How to programmatically add an MVC control to a page

Posted by Community Admin on 04-Aug-2018 16:52

How to programmatically add an MVC control to a page

All Replies

Posted by Community Admin on 24-Jun-2014 00:00

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 www.sitefinity.com/.../add-a-new-user-control-to-a-page but I don't know what the control path would be for my MVC widget.

Thanks.

Posted by Community Admin on 24-Jun-2014 00:00

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);
       

Posted by Community Admin on 24-Jun-2014 00:00

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;
       

This thread is closed