2011-09-14 39 views
5

Trong bộ khởi động bằng cách sử dụng trình xây dựng xpath, làm cách nào để nhận tất cả các mục kế thừa từ mẫu 'Site Section' trong mục Trang chủ?Lấy các mục bằng cách sử dụng truy vấn sitecore

Khi tôi chạy như sau:

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

một mục được trở /sitecore/content/Home/Products có ý nghĩa, tuy nhiên, sau đây không trả lại bất cứ điều gì:

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

Những gì tôi đang cố gắng làm là xây dựng một menu từ các mục kế thừa mẫu 'Site Section' bằng cách sử dụng điều khiển web asp.net thay vì xslt.

Bất kỳ ý tưởng nào?

Cảm ơn, Tarek

** CẬP NHẬT

Cung cấp thông tin thêm về câu hỏi:

mục /sitecore/content/Home/Products có mẫu /sitecore/templates/Starter Kit/Site Sections/Product Section trong đó có một mẫu cơ sở của /sitecore/templates/Starter Kit/Item Types/Site Section

Nếu tôi muốn các Sản phẩm và Tài liệu tham khảo (tương tự Sản phẩm) trong Trang chủ Tôi sẽ chạy truy vấn sau :

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

Có cách nào để tải mục trong Trang chủ có Phần trang web làm mẫu cơ sở không. Trong xslt có một phương thức sc:GetItemsOfType('site section',$home/item).

** trả lời

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

Trả lời

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

Những gì tôi sẽ đề nghị bạn phải làm là ghi logic để xác định xem một mục thực hiện một mẫu cụ thể. Bạn có thể làm điều này cho ví dụ sử dụng mã này:

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

Bạn cho này 'mục' và 'TEMPLATENAME' của mẫu cần kế thừa từ và bạn sẽ được trả lại một đúng/sai. Ví dụ, bạn có thể nhận được một danh sách và đi qua một foreach, trong đó bạn lọc ra các mục mà không phải chịu đựng.

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

Tôi hy vọng bạn có thể làm điều gì đó hữu ích với thông tin trên!

+0

Cảm ơn, đánh giá cao những chi tiết, nhưng mã cũng giống như chạy truy vấn:/Sitecore/content/home/* [@@ templatekey = 'phần sản phẩm'] Nó không chọn lên các mẫu cơ sở của phần sản phẩm phải là phần trang web. –

5

Sử dụng // trong truy vấn sẽ làm cho nó đệ quy khi / chỉ là trẻ em ngay lập tức. Điều này có tác động hiệu suất.

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

Ngoài ra, không nên là @@templatename và không @@templatekey?

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

mẫu khóa luôn thấp hơn templatename có thể có chữ hoa, nếu bạn đã đặt tên cho chúng bằng các ký tự chữ hoa, do đó, templatekey tốt hơn nên sử dụng. – Holger

+0

Cảm ơn nhưng điều này không trả về các mục chính xác. –

Các vấn đề liên quan