Retrieve items using sitecore query
Asked Answered
T

3

5

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);
}
Tergal answered 14/9, 2011 at 14:34 Comment(0)
T
5
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);
}
Tergal answered 23/9, 2011 at 21:18 Comment(0)
H
6

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']
Hoskinson answered 14/9, 2011 at 16:6 Comment(1)
templatekey is always lower case templatename can have uppercase letter, if you've named them with uppercase characters, so templatekey is better to use.Valeric
T
5
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);
}
Tergal answered 23/9, 2011 at 21:18 Comment(0)
H
0

What I would suggest you to do is write logic to determine if an item implements a specific template. You can do this for example using this code:

    /// <summary>
    /// Determines if the item implements the specified template.
    /// </summary>
    /// <param name="item">The item to check.</param>
    /// <param name="templateName">Name of the template.</param>
    /// <returns>
    /// A boolean indicating weather the item implements the template or not
    /// </returns>
    public static bool DoesItemImplementTemplate(Item item, string templateName)
    {
        if (item == null || item.Template == null)
        {
            return false;
        }

        var items = new List<TemplateItem> { item.Template };
        int index = 0;

        // flatten the template tree during search
        while (index < items.Count)
        {
            // check for match
            TemplateItem template = items[index];
            if (template.Name == templateName)
            {
                return true;
            }

            // add base templates to the list
            items.AddRange(template.BaseTemplates);

            // inspect next template
            index++;
        }

        // nothing found
        return false;
    }

You give this the 'item' and the 'templatename' of the template it should inherit from and you will be returned a true / false. For example you could get a List and go through a foreach in which you filter out the items that are not inherriting.

List<Item> completeList = new List<Item>();
completeList = Sitecore.Context.Item.Children().toList<Item>();

List<Item> correctItemList = new List<Item>();

foreach(Item thisItem in completeList) 
{
   if(DoesItemImplementTemplate(thisItem, "myTemplate") 
   {
      if(!correctItemList.Contains(thisItem) 
      {
         correctItemList.Add(thisItem);
      }
   }
}

I hope you can make something useful with the above information!

Harmonie answered 14/9, 2011 at 14:49 Comment(1)
Thanks, appreciate the details, but the code is the same as running query: /sitecore/content/home/*[@@templatekey='product section'] It's not picking up the base template of the product section which should be site section.Tergal

© 2022 - 2024 — McMap. All rights reserved.