2011-09-14 6 views
5

xpathビルダーを使用するスターターキットでは、[ホーム]アイテムの[サイトセクション]テンプレートから継承するすべてのアイテムを取得するにはどうすればよいですか?sitecoreクエリを使用してアイテムを取得する

私は次のコマンドを実行します。

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

つの項目が理にかなっている/sitecore/content/Home/Productsを返されたが、次は何も返しません:私は何をしようとしている

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

ですxsltではなくasp.net Webコントロールを使用して 'Site Section'テンプレートを継承するアイテムからメニューを構築します。

アイデア?

おかげで、 タレク

** はUPDATE

は、質問に関する詳しい情報を提供します

項目/sitecore/content/Home/Productsは私がした場合/sitecore/templates/Starter Kit/Item Types/Site Section

の基本テンプレートを持つテンプレート/sitecore/templates/Starter Kit/Site Sections/Product 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」与え、あなたは真/偽が返されます:あなたはこのコードを使用して、たとえば、これを行うことができます。たとえば、リストを取得し、foreherを実行して、継承していない項目を除外することができます。

上記の情報を参考にしてください。

+0

おかげで、詳細を感謝しますが、コードは、クエリを実行するのと同じですサイトセクションでなければならない製品セクションの基本テンプレート。 –

5

クエリで//を使用すると、再帰的になります。/は直ちに子のみです。これはパフォーマンスに影響します。

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

また、それは@@templatenameとない@@templatekeyすべきではありませんか?それが拾っていない/サイトコア/コンテンツ/ホーム/ * [@@ templatekey =「製品セクション」] :

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

templatekeyは常に小文字です templatenameには大文字を使用できます(大文字で名前を付けた場合はtemplatekeyを使用する方がよい)。 – Holger

+0

ありがとうございますが、これは正しい項目を返しません。 –

関連する問題