Retrieve items using sitecore query
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 qu开发者_运维问答estion:
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);
}
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']
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);
}
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!
精彩评论