2011-09-14 10 views
5

Nello starter kit utilizzando il builder xpath, come ottengo tutti gli elementi che ereditano dal modello "Sezione sito" sotto l'elemento Home?Recupera elementi utilizzando la query sitecore

Quando eseguo il seguente:

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

un articolo è restituito /sitecore/content/Home/Products che ha un senso, tuttavia, il seguente non restituisce nulla:

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

Quello che sto cercando di fare è crea un menu dagli elementi che ereditano il modello 'Site Section' usando il controllo web asp.net invece di xslt.

Qualche idea?

Grazie, Tarek

** AGGIORNAMENTO

fornire ulteriori informazioni sulla questione:

Articolo /sitecore/content/Home/Products ha template /sitecore/templates/Starter Kit/Site Sections/Product Section che ha un modello di base di /sitecore/templates/Starter Kit/Item Types/Site Section

Se io voglio gli articoli Prodotti e referenze (simili ai prodotti) in Home Vorrei eseguire la seguente query :

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

C'è un modo per ottenere l'elemento sotto Home che ha Sezione sito come modello di base. In xslt esiste un metodo sc:GetItemsOfType('site section',$home/item) che lo fa.

** risposta

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); 
} 

risposta

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); 
} 
0

Quello che vorrei suggerire di fare è la logica di scrittura per determinare se un elemento implementa un modello specifico. È possibile farlo ad esempio utilizzando questo codice:

/// <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; 
    } 

di dare a questo la 'voce' e il 'TemplateName' del modello dovrebbe ereditare da e ti verrà restituito un vero/falso. Ad esempio è possibile ottenere una lista e passare attraverso un foreach in cui si filtrano gli elementi che non sono ereditari.

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); 
     } 
    } 
} 

Spero che tu possa fare qualcosa di utile con le informazioni di cui sopra!

+0

Grazie, apprezzare i dettagli, ma il codice è la stessa cosa che lanciare query:/Sitecore/content/home/* [@@ templatekey = 'sezione prodotti'] Non è prendere in mano il modello di base della sezione prodotto che dovrebbe essere la sezione del sito. –

5

L'utilizzo di // nella query lo renderà ricorsivo laddove come / è solo per bambini immediati. Questo ha impatti sulle prestazioni.

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

Inoltre, non dovrebbe essere @@templatename e non @@templatekey?

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

templatekey è sempre in minuscolo templatename può avere la lettera maiuscola, se li hai nominati con caratteri maiuscoli, quindi templatekey è meglio usare. – Holger

+0

Grazie, ma questo non restituisce gli articoli corretti. –