Get Placeholders

Get Placeholders

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • System.Collections.Generic.List
  • Sitecore.Context
  • Sitecore.Diagnostics.Assert
  • Sitecore.Data.Fields.LayoutField
  • Sitecore.Data.Items.DeviceItem
  • Sitecore.Data.Items.Item
  • Sitecore.Layouts.RenderingReference

Placeholders C# Web Control Code

Sitecore.Data.Items.DeviceItem device = Sitecore.Context.Device;
Sitecore.Diagnostics.Assert.IsNotNull(device, "Device could not be found");

Sitecore.Data.Items.Item item = this.GetItem();
Sitecore.Diagnostics.Assert.IsNotNull(item, "Item could not be found");

Sitecore.Data.Fields.LayoutField layoutField = item.Fields["__renderings"];
if (layoutField == null) return;

Sitecore.Layouts.RenderingReference[] renderings = layoutField.GetReferences(device);
if (renderings == null) return;

System.Collections.Generic.List<string> placeholders = new System.Collections.Generic.List<string>();

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.Write("C# Web Control Real-time Output");
output.RenderEndTag();

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-size:x-small; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
//using &#58; for colon and &#160; for space
output.Write("{0}&#58;&#160;{1}", "Item", item.DisplayName);
output.RenderEndTag(); //P

foreach (var rendering in renderings)
{
    if (!placeholders.Contains(rendering.Placeholder))
    {
        placeholders.Add(rendering.Placeholder);
        output.Write("{0}", rendering.Placeholder + "<br/>");
    }
}

output.RenderEndTag(); //Div


Placeholders C# Web Control Output

header
HeadLowerRt
mainbody
L2BSL
L2BLC
L2BSR
footer