This Get Context

This Get Context

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Context
  • Sitecore.Data.Database
  • Sitecore.Data.Items.Item

Context C# Web Control Code

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

Sitecore.Data.Database db = Sitecore.Context.Database;
Sitecore.Data.Items.Item devItem = db.GetItem("/sitecore/Content/Home/Marketers");
if (devItem == 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);
//using : for colon and ? for question mark and   for space
output.Write("{0}: {1}", "Item", item.DisplayName);
output.RenderEndTag(); //P

Sitecore.Data.Items.Item previousSibling = null;
if (devItem.Axes.GetPreviousSibling() != null)
{
    previousSibling = devItem.Axes.GetPreviousSibling();
    output.Write("{0}&#58;&#160;{1}", "Previous Sibling", "<b>" + previousSibling.Name + "</b><br/>");
}

Sitecore.Data.Items.Item nextSibling = null;
if (devItem.Axes.GetNextSibling() != null)
{
    nextSibling = devItem.Axes.GetNextSibling();
    output.Write("{0}&#58;&#160;{1}", "Next Sibling", "<b>" + nextSibling.Name + "</b><br/>");
}

string isAncestor = item.Axes.IsAncestorOf(devItem).ToString();
output.Write("{0}&#63;&#160;{1}", "Ancestor of 'Developers' node", "<b>" + isAncestor + "</b><br/>");

string isDescendant = item.Axes.IsDescendantOf(devItem).ToString();
output.Write("{0}&#63;&#160;{1}", "Descendant of 'Developers' node", "<b>" + isDescendant + "</b>");


Context C# Web Control Output

Previous Sibling: ContentAuthors
Next Sibling: WebDesigners
Ancestor of 'Developers' node? False
Descendant of 'Developers' node? False