Retrieve items using sitecore query

user623892 picture user623892 · Sep 14, 2011 · Viewed 12.9k times · Source

In the starter kit using xpath builder, how do I get all the items that inherit from the 'Site Section' template under the Home item?

When I run the following:

/sitecore/content/home/*[@@templatekey='product section'] 

one item is returned /sitecore/content/Home/Products which makes sense, however, the following does not return anything:

/sitecore/content/home/*[@@templatekey='site section'] 

What I'm trying to do is build a menu from the items that inherit the 'Site Section' template using asp.net web control instead of xslt.

Any ideas?

Thanks, Tarek

** UPDATE

Provide more info on the question:

Item /sitecore/content/Home/Products has template /sitecore/templates/Starter Kit/Site Sections/Product Section which has a base template of /sitecore/templates/Starter Kit/Item Types/Site Section

If I want the Products and References (similar to Products) items under Home I would run the following query:

/sitecore/content/home/*[@@templatekey='product section' or @@templatekey='references section']

Is there a way to get the item under Home that has Site Section as the base template. In xslt there is a method sc:GetItemsOfType('site section',$home/item) which does it.

** Answer

var homeItem = Sitecore.Context.Database.GetItem(Sitecore.Context.Site.StartPath);
var siteSectionItems = new List<Item>();

foreach (Item item in homeItem.Children)
{
    var itemTemplate = TemplateManager.GetTemplate(item);

    if (itemTemplate.InheritsFrom("Site Section"))
        siteSectionItems.Add(item);
}

Answer

Mark Ursino picture Mark Ursino · Sep 14, 2011

Using // in the query will make it recursive where as / is immediate children only. This has performance impacts.

/sitecore/content/home//*[@@templatekey='site section'] 

Also, shouldn't it be @@templatename and not @@templatekey?

/sitecore/content/home//*[@@templatename='site section']