Sort By Field

Sort By Field

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Data.Items.Item
  • Sitecore.Links.LinkManager
  • System.Linq

Sort By Field C# Web Control Code

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

Sitecore.Data.Items.Item ancestor = this.GetItem().Parent.Parent.Parent;
if (ancestor == null) return;

Sitecore.Data.Items.Item[] descendants = ancestor.Axes.GetDescendants();
if (descendants == null) return;

var sortedList = (from entry in descendants orderby entry.Fields["title"].Value ascending select entry);

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 space
output.Write("{0}: {1}", "Item", item.DisplayName);
output.RenderEndTag(); //P

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
output.Write("{0}", "First Ten Code Snippet Pages Sorted by Title");
output.RenderEndTag(); //P

int i = 0;
foreach (Sitecore.Data.Items.Item snip in sortedList)
{
    if (snip.TemplateName == "TIC-CodeSnippet")
    {
        i++;
        output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Href, Sitecore.Links.LinkManager.GetItemUrl(snip));
        output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.A);
        if (snip.Fields["Title"] != null)
output.Write("{0}", snip.Fields["Title"] + "<br/>");
        output.RenderEndTag();  //A
    }
    //only display first 10
    if (i >= 10) break;
}

output.RenderEndTag(); //Div
        }


Sort By Field C# Web Control Output