Using Sitecore Query

Using Sitecore Query

Sitecore Query

  • Sitecore resolves queries with predicates in the data manager using the query API
  • All items in the query scope (all descendants) are loaded so that the predicate can be evaluated against each item and the matching items returned
  • Sitecore Query is much slower than Sitecore Fast Query, which uses straight SQL queries
  • The SelectItems() methods require a Sitecore Query as a parameter and return an item array

Sitecore Query Syntax

  • Prefix query expressions with query: when used to filter the source property of fields
  • Omit the "query:" keyword when using Sitecore query expressions in code or testing in the XPath Builder
  • Surround template names that contain spaces with "#"
  • Separate multiple parameters in the source property with "&"

Sitecore Query Guidelines

  • Use the Sitecore Search API instead of Sitecore Query so that indexes are used rather than the content structure
  • Use the XPath Builder in the Developer Center Tools menu to build and test Sitecore query expressions before implementing them in code or the source properties of fields

Field Types that support Sitecore Query

  • Checklist
  • Droplink
  • Droplist
  • Grouped Droplink
  • Grouped Droplist
  • Multilist

Field Types that do not support Sitecore Query

  • DropTree
  • TreeList
  • TreeListEx

Sitecore Query C# Web Control Code

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

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

const string filter = ".//*[@@templatename='TIC-ConfigSetting']";
Sitecore.Data.Items.Item[] descendants = ancestor.Axes.SelectItems(filter);
if (descendants == 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 displayName = Sitecore.StringUtil.GetString(item.DisplayName, item.Name);
//using : for colon and   for space
output.Write("{0}: {1}", "Item", displayName);
output.RenderEndTag(); //P

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
output.Write("{0}", "Filtered Descendants");
output.RenderEndTag(); //P

int i = 0;
foreach (Sitecore.Data.Items.Item descendant in descendants)
{
    i++;
    output.Write("{0}", descendant.DisplayName + "<br/>");
    //only display first ten items
    if (i >= 10) break;
}

output.RenderEndTag(); //Div


Sitecore Query XSLT Rendering Code

<xsl:variable name="ancestor" select="../.." />

<div style="font-weight:bold; width:100%; clear:both"> <br/>
<xsl:value-of select="'XSLT Rendering Real-time Output'"/>
</div>

<div style="font-size:x-small; width:100%; clear:both">

<p><b><xsl:value-of select="'Item: '" /><sc:text field="title" select="$sc_item" /></b></p>
<p><xsl:value-of select="'Filtered Descendants'" /></p>

<xsl:for-each select="$ancestor//*[@template='tic-configsetting']">
   <xsl:if test="position()&lt; 11">  
      <xsl:value-of select="./@name"/><br/>
   </xsl:if>
</xsl:for-each>

</div>

Sitecore Query C# Web Control Output

Filtered Descendants

Markup In Fields
Icon Settings
Item Settings
Valid Item Names
Lock Settings
Item Not Found
UrlOptions Defaults
Using LinkManager
Query Settings
Setting Max Returns

Sitecore Query XSLT Rendering Output

Filtered Descendants

MarkupInFields
IconSettings
ItemSettings
ValidItemNames
LockSettings
ItemNotFound
UrlOptionsDefaults
UsingLinkManager
QuerySettings
SettingMaxReturns

Notes

  • This code example only displays the first ten items found by the query

Sources