Working with Layout Controls Programmatically

Working with Layout Controls Programmatically

November 08, 2013 0 Comments

The content you're reading is getting on in years
This post is on the older side and its content may be out of date.
Be sure to visit our blogs homepage for our latest news, updates and information.

Sitefinity's backend UI allows adding various configurations for layout controls to control the page layout display and in this blog post I will provide a sample on how to create new layout controls on a page using the API and how to query layout controls on pages.

1. To create Layout control on a page refer to the sample method below, it refers to this documentation sample and extends it.

public void AddControlToPage(string pageTitle, string placeHolder, string caption)
       {
           var pageManager = PageManager.GetManager();
           var page = pageManager.GetPageNodes().Where(p => p.Title == pageTitle).SingleOrDefault();
  
           if (page != null)
           {
               var temp = pageManager.EditPage(page.Page.Id);
               Guid controlID = Guid.NewGuid();
               if (temp != null)
               {
                   var pageControl = pageManager.CreateControl<PageDraftControl>(controlID);
                   pageControl.Caption = caption;
                   pageControl.ObjectType = "Telerik.Sitefinity.Web.UI.LayoutControl, Telerik.Sitefinity";
  
                   pageControl.PlaceHolder = placeHolder;
                   pageControl.IsLayoutControl = true;
                   pageManager.SetControlDefaultPermissions(pageControl);
  
  
                   var prop = pageManager.CreateProperty();
                   prop.Name = "Layout";
                   prop.Value = "~/SFRes/Telerik.Sitefinity.Resources.Templates.Layouts.Column1Template.ascx";
                   pageControl.Properties.Add(prop);
                     
  
                   temp.Controls.Add(pageControl);
                   pageManager.PagesLifecycle.CheckIn(temp);
                   pageManager.SaveChanges();
  
                   var bag = new Dictionary<string, string>();
                   bag.Add("ContentType", typeof(PageNode).FullName);
                   WorkflowManager.MessageWorkflow(page.Id, typeof(PageNode), null, "Publish", false, bag);
  
               }
           }
       }

The code creates a new control in the page and sets its ObjectType to the type of the Sitefinity layout controls.

var pageControl = pageManager.CreateControl<PageDraftControl>(controlID);
                   pageControl.Caption = caption;
                   pageControl.ObjectType = "Telerik.Sitefinity.Web.UI.LayoutControl, Telerik.Sitefinity";

The other required property to make the PageDraftControl layout control is 

pageControl.IsLayoutControl = true;

Each layout control is different and to make the control place specific layout control on the page, you need to create a new property for the control with Name "Layout" and as value pass the path to the layout control template. All layout control paths can be found in Administration->Settings->Advanced->Toolboxes->Toolboxes->PageLayouts->Sections->TwoCol. To select the layout control you want to use, refer to its Layout Template textbox.

var prop = pageManager.CreateProperty();
                    prop.Name = "Layout";
                    prop.Value = "~/SFRes/Telerik.Sitefinity.Resources.Templates.Layouts.Column1Template.ascx";
                    pageControl.Properties.Add(prop);


2. Once added, the layout controls can be queried so you can find what is the placeholder id of each layout control and use it to place controls into it. To query layout controls based on different properties refer to the sample below.

public void QueryLayoutControls(string pageTitle)
        {
            var pageManager = PageManager.GetManager();
            var page = pageManager.GetPageNodes().Where(p => p.Title == pageTitle).SingleOrDefault();
            if (page != null)
            {
                Guid controlID = Guid.NewGuid();
               //find layout control on a page by one of the shown properties
                    var pageControls = pageManager.GetControls<PageDraftControl>()
                                            .Where(c=>c.ObjectType.Equals("Telerik.Sitefinity.Web.UI.LayoutControl, Telerik.Sitefinity")
                                            && c.Caption.Equals("100%")
                                            && c.IsLayoutControl == true);
 
                    foreach (var control in pageControls)
                    {
                        var placeholder = control.PlaceHolder;
                        var placeholders = control.PlaceHolders;
                        var property = control.Properties;
                        var cap = control.Caption;
                        //here you can the name of the .ascx file used for this control
                        var nameProp = property.Where(pro=> pro.Name.Equals("Layout"));
                    }
            }
        }

In the query for retrieving layout controls I have used several properties to demo the available ways to query layout controls by different property to server different cases:

ObjectType.Equals("Telerik.Sitefinity.Web.UI.LayoutControl, Telerik.Sitefinity")

 For caption enter the name of the layout control used. If the control is called "CustomLayoutControl", in its caption use this name to find specific controls:

c.Caption.Equals("100%")

 The placeholder name which each layout control produces is the property "placeholderId". Use this placeholder name to nest other controls in this layout control.

var placeholders = control.PlaceHolders;

Stanislav Velikov

Stanislav Velikov is a Tech Support Engineer at Telerik. He joined the Sitefinity Support team in April 2011.

Comments
Comments are disabled in preview mode.
Topics
 
 
Latest Stories in
Your Inbox
Subscribe
More From Progress
d12fcc0bdb669b804e7f71198c9619a7
5 Questions Automakers Should Ask to Improve Asset Uptime
Download Whitepaper
 
SF_MQ_WCM
2018 Gartner Magic Quadrant Web Content Management (WCM)
Download Whitepaper
 
What-Serverless-Means-For-Enterprice-Apps-Kinvey
What Serverless Means for Enterprise Apps
Watch Webinar