Rendering Items

Rendering Items

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Context
  • Sitecore.Data.Database
  • Sitecore.Data.Items.RenderingItem
  • Sitecore.Data.Items.Item
  • Sitecore.Diagnostics.Log
  • Sitecore.StringUtil
  • System.Exception

Rendering Items C# Web Control Code

Sitecore.Data.Items.Item item = Sitecore.Context.Item;
if (item == null) return;

Sitecore.Data.Database db = Sitecore.Context.Database;
Sitecore.Data.Items.RenderingItem[] renderings = db.Resources.Renderings.GetAll();
if (renderings == null) return;

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);
string itemName = Sitecore.StringUtil.GetString(item.DisplayName, item.Name);
//using : for colon and   for space
output.Write("{0}: {1}", "Item", itemName);
output.RenderEndTag(); //P

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Border, "0");
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Cellpadding, "2");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Table);
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Thead);
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Align, "center");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Th);
output.Write("Rendering Item");
output.RenderEndTag(); //Th
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Align, "center");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Th);
output.Write("Tag Name");
output.RenderEndTag(); //Th
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Align, "center");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Th);
output.Write("Tag Prefix");
output.RenderEndTag(); //Th
output.RenderEndTag(); //Thead
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Tbody);

int iRendering = 0;
foreach (Sitecore.Data.Items.RenderingItem rendering in renderings)
{
    try
    {
        iRendering++;

        output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Tr);
        output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Align, "left");
        output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Td);
        output.Write("{0}", rendering.Name);
        output.RenderEndTag(); //Td

        output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Align, "left");
        output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Td);
        output.Write("{0}", rendering.TagName);
        output.RenderEndTag(); //Td

        output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Align, "left");
        output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Td);
        output.Write("{0}", rendering.TagPrefix);
        output.RenderEndTag(); //Td

        output.RenderEndTag(); //Tr

        //only show the first 15 renderings
        if (iRendering >= 15) break;
    }

    catch (System.Exception ex)
    {
        output.RenderEndTag(); //Td
        output.RenderEndTag(); //Tr
        Sitecore.Diagnostics.Log.Error("Exception Message: " + ex.Message + " Target Site: " + ex.TargetSite, this);
    }
      
}
output.RenderEndTag(); //Tbody
output.RenderEndTag(); //Table
output.RenderEndTag(); //Div


Rendering Items C# Web Control Output

Rendering Item Tag Name Tag Prefix
Sample Rendering XslFile sc
FieldRenderer FieldRenderer sc
WebEdit Content Editor WebEditContentEditor sc
WebEdit Ribbon WebEditRibbon sc
Hidden Rendering XslFile sc
FeedRenderer FeedRenderer sc
Clear DB Caches
Clear Site Caches
Make File Backup
Reset Renderings ResetRenderings tic
Unlock All Items
Unlocking Items
Clear All Caches
Replace Values
Get Config State GetConfigState tic

Notes

  • This code example only displays the first 15 renderings it finds