2011-09-14 13 views
5

एक्सपैथ बिल्डर का उपयोग करके स्टार्टर किट में, मैं होम आइटम के अंतर्गत 'साइट अनुभाग' टेम्पलेट से प्राप्त सभी आइटम कैसे प्राप्त करूं?साइटकोर क्वेरी का उपयोग कर आइटम पुनर्प्राप्त करें

जब मैं निम्नलिखित चलाएँ:

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

एक आइटम /sitecore/content/Home/Products जो समझ में आता है लौटा दिया जाता है, फिर भी, निम्नलिखित कुछ भी वापस नहीं करता है:

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

मुझे क्या करना कोशिश कर रहा हूँ है xslt के बजाय asp.net वेब नियंत्रण का उपयोग कर 'साइट अनुभाग' टेम्पलेट का उत्तराधिकारी आइटम से मेनू बनाएं।

कोई विचार?

धन्यवाद, तारेक

** अद्यतन

सवाल बारे में अधिक जानकारी प्रदान करें:

आइटम /sitecore/content/Home/Products टेम्पलेट /sitecore/templates/Starter Kit/Site Sections/Product Section जो /sitecore/templates/Starter Kit/Item Types/Site Section

का आधार टेम्पलेट है, तो है मैं होम के तहत उत्पाद और संदर्भ (उत्पाद के समान) आइटम चाहते हैं, मैं निम्नलिखित क्वेरी चलाऊंगा :

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

वहाँ एक रास्ता होम कि आधार टेम्पलेट के रूप में साइट धारा है के तहत आइटम प्राप्त करने के लिए है। Xslt में एक विधि sc:GetItemsOfType('site section',$home/item) है जो यह करता है।

** उत्तर

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

उत्तर

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

मैं आपको बस इतना करना सुझाव है कि क्या यह निर्धारित करने के लिए एक आइटम एक विशिष्ट टेम्पलेट लागू करता है लिखने तर्क है। आप इस कोड का उपयोग उदाहरण के लिए ऐसा कर सकते हैं:

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

आप इस 'आइटम' और टेम्पलेट यह से विरासत चाहिए की 'TEMPLATENAME' देने के लिए और आप एक सही/गलत लौटा दी जाएगी। उदाहरण के लिए आप एक सूची प्राप्त कर सकते हैं और एक foreach के माध्यम से जा सकते हैं जिसमें आप उन वस्तुओं को फ़िल्टर करते हैं जो विरासत में नहीं हैं।

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

मुझे आशा है कि आप उपर्युक्त जानकारी के साथ कुछ उपयोगी बना सकते हैं!

+0

धन्यवाद, विवरण की सराहना करते हैं, लेकिन कोड क्वेरी चलाने के समान ही है:/sitecore/सामग्री/घर/* [@@ templatekey = 'उत्पाद खंड'] यह उठा नहीं कर रहा है उत्पाद खंड का आधार टेम्पलेट जो साइट अनुभाग होना चाहिए। –

5

क्वेरी में // का उपयोग करके यह रिकर्सिव हो जाएगा जहां / केवल तत्काल बच्चे हैं। इसका प्रदर्शन प्रभाव पड़ता है।

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

इसके अलावा, यह @@templatename और नहीं @@templatekey नहीं होना चाहिए?

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

टेम्पलेटकी हमेशा कम मामला templatename में अपरकेस अक्षर हो सकता है, अगर आपने उन्हें अपरकेस वर्णों के साथ नाम दिया है, तो टेम्पलेटकी उपयोग करने के लिए बेहतर है। – Holger

+0

धन्यवाद लेकिन यह सही आइटम नहीं लौटाता है। –

संबंधित मुद्दे