3

次の DWT TBB で出力を使用するために、C# TBB を使用してカテゴリに存在するキーワードを取得しようとしています。

そのために、Category フィールドを持つコンポーネントがあります。

キーワード値を取得するために、次の C# TBB を記述しようとしています。

<%@Import NameSpace="Tridion.ContentManager.Templating.Expression" %>        

try
{
    string className = package.GetValue("Component.Fields.title");  
    KeywordField keywordField = package.GetKeywordByTitle(className);

    package.PushItem("Class", package.CreateStringItem(ContentType.Text, keywordField.Value.Key));


}
catch(TemplatingException ex)
{
    log.Debug("Exception is " + ex.Message);
}

しかし、次のコンパイルエラーが発生します。

次の理由により、テンプレートをコンパイルできませんでした: エラー CS0246: 型または名前空間名 'KeywordField' が見つかりませんでした (using ディレクティブまたはアセンブリ参照がありませんか?) エラー CS1061: 'Tridion.ContentManager.Templating.Package' に含まれていません「GetKeywordByTitle」の定義と、タイプ「Tridion.ContentManager.Templating.Package」の最初の引数を受け入れる拡張メソッド「GetKeywordByTitle」が見つかりませんでした (using ディレクティブまたはアセンブリ参照がありませんか?)

どうすればそれを達成できるか教えてください。

前もって感謝します

4

3 に答える 3

4

Jeremy が API を勉強すべきだと提案したので、カテゴリからキーワードを取得する例を紹介します。それが役立つことを願っています

インクルード ファイル

using Tridion.ContentManager;
using Tridion.ContentManager.CommunicationManagement;
using Tridion.ContentManager.Templating.Assembly;
using Tridion.ContentManager.ContentManagement;
using Tridion.ContentManager.ContentManagement.Fields;
using Tridion.ContentManager.Templating;

サンプルコード、要件に応じて、ここでループからキーと値を使用できます。

string catID = package.GetByName("CategoryID").GetAsString();
        TcmUri catURI = new TcmUri(int.Parse(catID), ItemType.Category, PubId);
        var theCategory = m_Engine.GetObject(catURI) as Category;
        catKeywords = GetCatKeywords(theCategory);
        string strSelect = "<select>";
        foreach (Keyword k in catKeywords)
        {

        k.Key  // Keyowrd key
        k.Value // KEyword Value

        }

//keyword list  
private IList<Keyword> GetCatKeywords(Category category)
{
    IList<Keyword> keywords;

    if (!Utilities.IsNull(category))
    {
        Filter filter = new Filter();
        filter.BaseColumns = ListBaseColumns.IdAndTitle;
        keywords = category.GetKeywords(filter);

        if (!Utilities.IsNull(keywords))
        {
            return keywords;
        }
    }

    return null;
}
于 2012-10-16T11:16:58.140 に答える
3

エラーメッセージは、問題が何であるかを完全に明確にしています。KeywordFieldクラスへの参照はありません。関連する名前空間をインポートする必要があります。

<%@Import NameSpace="Tridion.ContentManager.ContentManagement.Fields" %>

また、PackageオブジェクトにGetKeywordByTitleというメソッドがないことも絶対に明らかです。GetByNameメソッドがありますが、これはパッケージから名前付きアイテムを取得するためのものであり、リポジトリからオブジェクトを取得するためのものではありません。

Tridion.ContentManager.ContentManagement.CategoryにはGetKeywordByTitleメソッドがありますが、これを使用するには、最初にカテゴリを取得する必要があります。これは、カテゴリのURIを知っている必要があることを意味します。

おそらく、APIドキュメントをもう少し勉強する必要がありますか?

于 2012-10-16T08:23:39.607 に答える
0

「GetKeywordByTitle」はPackageのメソッドではなく、Categoryのメソッドです。あなたはただ新しいキーワードではありませんか?

string selectedKeyword= package.GetValue("Component.Fields.title");
Keyword keyword = new Keyword(selectedKeyword, engine.GetSession());

乾杯

于 2012-10-16T08:25:11.000 に答える