SelectItems Method

SelectItems Method

Sitecore.Data.Database.SelectItems()

  • Requires Sitecore Query expression
  • Retrieves an item array
  • Can be sorted with Comparer

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

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

C# Web Control Code

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

const string query = "/sitecore/content/home//*[@@templatename='TIC-TacticalPage']";
Sitecore.Data.Database db = Sitecore.Context.Database;
Sitecore.Data.Items.Item[] items = db.SelectItems(query);
if (items == null) return;

//using shared source module FieldValueComparer class
System.Array.Sort<Sitecore.Data.Items.Item>(items, new Website.layouts.TIC.Classes.FieldValueComparer("menu title"));

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.DisplayName, " Tactical Items");
output.RenderEndTag(); //P

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Ul);
foreach (Sitecore.Data.Items.Item selectItem in items)
{
    output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Li);
    //using &#58; for colon and &#160; for space
    output.Write("{0}", "<b>" + selectItem.DisplayName + "</b>");
    output.RenderEndTag(); //Li
}
output.RenderEndTag(); //Ul
output.RenderEndTag(); //Div


Notes

  • Using shared source module FieldValueComparer in this code

Sources