Add a layout control to a page

NEW TO SITEFINITY?

The following example adds the layout control to a specified page:

using System;
using System.Linq;
using Telerik.Sitefinity.Localization;
using Telerik.Sitefinity.Modules.Pages;
using Telerik.Sitefinity.Pages.Model;
using Telerik.Sitefinity.Services;
using Telerik.Sitefinity.Web.UI;
namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Pages
{
public partial class PagesSnippets
{
public static void AddLayoutControlsToPage()
{
var pageId = Guid.Parse("53A5B8DC-5BB0-655C-89B0-FF00000EB5DE");
var pageManager = PageManager.GetManager();
var pageData = pageManager.GetPageNode(pageId).GetPageData();
var headerPlaceholder = GetHeaderPlaceholderId(pageData);
var contentPlaceholder = GetContentPlaceholderId(pageData);
var headerCaption = "LayoutControlInHeaderPlaceholder";
var contentCaption = "LayoutControlInContentPlaceholder";
var layoutResource = "~/SFRes/Telerik.Sitefinity.Resources.Templates.Layouts.Column2Template2.ascx";
PagesSnippets.AddLayoutControlToPage(pageId, headerPlaceholder, headerCaption, layoutResource);
PagesSnippets.AddLayoutControlToPage(pageId, contentPlaceholder, contentCaption, layoutResource);
}
public static string GetHeaderPlaceholderId(PageData pageData)
{
var headerControl = pageData.Template.Controls.FirstOrDefault(x => x.Caption.Contains("Header"));
// Col00 means the first column. As this control has only one column the number is 00;
var headerControlAsPlaceholderId = headerControl.Properties.FirstOrDefault(x => x.Name == "ID").Value + "_Col00";
return headerControlAsPlaceholderId;
}
public static string GetContentPlaceholderId(PageData pageData)
{
var contentAndSideBar = pageData.Template.Controls.FirstOrDefault(x => x.Caption.Contains("Content"));
// You can specify which column to get ( e.g. Content & Right Sidebar)
// using the PlaceHolders collection from the control itself.
var contentPlaceHolder = contentAndSideBar.PlaceHolders[0];
var rigthSideBarPlaceHolder = contentAndSideBar.PlaceHolders[1];
return contentPlaceHolder;
}
public static void AddLayoutControlToPage(Guid pageId, string placeholder, string caption, string layoutResource)
{
using (new CultureRegion(SystemManager.CurrentContext.Culture))
{
var manager = PageManager.GetManager();
var pageData = manager.GetPageNode(pageId).GetPageData();
var temp = manager.EditPage(pageData.Id);
var layoutControl = PagesSnippets.CreateLayoutControl(manager, placeholder, caption, layoutResource);
manager.SetControlId(temp, layoutControl);
// Add the control to the page
temp.Controls.Add(layoutControl);
var master = manager.PagesLifecycle.CheckIn(temp);
manager.PagesLifecycle.Publish(master);
manager.SaveChanges();
}
}
public static PageDraftControl CreateLayoutControl(PageManager manager, string placeHolder, string caption, string layoutResource)
{
var pageControl = manager.CreateControl<PageDraftControl>();
pageControl.Caption = caption;
pageControl.ObjectType = typeof(LayoutControl).FullName;
pageControl.PlaceHolder = placeHolder;
pageControl.IsLayoutControl = true;
manager.SetControlDefaultPermissions(pageControl);
var prop = manager.CreateProperty();
prop.Name = "Layout";
prop.Value = layoutResource;
pageControl.Properties.Add(prop);
return pageControl;
}
}
}

Using the code above you add a layout control to the Header, Content, Right Sidebar, or Footer of a page that inherits from a template that has these controls. The layout control uses the template control as a placeholder. Thus, you are able to break an entire row into columns.

To place a layout control in a placeholder, you first need to find that placeholder. Depending on the template control, you can use the Placeholders collection from the control itself or select the Id value from the control Properties collection.
Then, you initialize the PageManager and based on the page Id you get the page data. To add a layout control, you must create the temp draft of the page. To do this, you use the EditPage method and specify the Id of the page. Then, you create the layout control using the CreateControl<PageDraftControl> method of the PageManager. You then specify all the necessary properties for the layout control (the caption, object type, placeholder, and so on). You set the default permissions by calling SetControlDefaultPermissions method. Then, you add the control to the temp draft page and call CheckIn to get the master. Finally, you publish the page using the WorkflowManager class and pass the required parameters to the MessageWorkflow method.

Want to learn more?

Increase your Sitefinity skills by signing up for our free trainings. Get Sitefinity-certified at Progress Education Community to boost your credentials.

Get started with Integration Hub | Sitefinity Cloud | Sitefinity SaaS

This free lesson teaches administrators, marketers, and other business professionals how to use the Integration hub service to create automated workflows between Sitefinity and other business systems.

Web Security for Sitefinity Administrators

This free lesson teaches administrators the basics about protecting yor Sitefinity instance and its sites from external threats. Configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.

Foundations of Sitefinity ASP.NET Core Development

The free on-demand video course teaches developers how to use Sitefinity .NET Core and leverage its decoupled architecture and new way of coding against the platform.

Was this article helpful?