Tridion 如何从C#TBB中的类别名称中获取关键字?

Tridion 如何从C#TBB中的类别名称中获取关键字?,tridion,tridion-2011,Tridion,Tridion 2011,我试图使用C#TBB获取类别中的关键字,以使用以下DWT TBB中的输出 为此,我有一个带有Category字段的组件 我试图编写以下C#TBB来获取关键字值 尝试 { 字符串className=package.GetValue(“Component.Fields.title”); 关键字字段关键字字段=package.GetKeywordByTitle(类名); package.PushItem(“Class”,package.CreateStringItem(ContentType.Tex

我试图使用C#TBB获取类别中的关键字,以使用以下DWT TBB中的输出

为此,我有一个带有Category字段的组件

我试图编写以下C#TBB来获取关键字值


尝试
{
字符串className=package.GetValue(“Component.Fields.title”);
关键字字段关键字字段=package.GetKeywordByTitle(类名);
package.PushItem(“Class”,package.CreateStringItem(ContentType.Text,keywordField.Value.Key));
}
捕获(模板例外)
{
log.Debug(“异常为”+ex.Message);
}
但我得到以下编译错误

无法编译模板,因为:错误CS0246:找不到类型或命名空间名称“KeywordField”(是否缺少using指令或程序集引用?)错误CS1061:“Tridion.ContentManager.Templating.Package”不包含“GetKeywordByTitle”的定义,并且找不到接受类型为“Tridion.ContentManager.Templating.Package”的第一个参数的扩展方法“GetKeywordByTitle”(是否缺少using指令或程序集引用?)

请建议我如何实现它


提前感谢

错误消息完全清楚问题所在-没有对KeywordField类的引用。您需要导入相关的命名空间:


另外,包对象没有名为GetKeywordByTitle的方法这一事实也是绝对清楚的。有一个GetByName方法,但它用于从包中检索命名项,而不是从respository中获取对象

Tridion.ContentManager.ContentManagement.Category确实有一个GetKeywordByTitle方法,但要使用它,您必须首先获取类别,这可能意味着必须知道类别的URI

也许您需要进一步研究API文档?

“GetKeywordByTitle”不是包上的方法,而是类别上的方法。 你就不能换个新的关键字吗

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

干杯

Af Jeremy建议您应该学习API,我为您提供了从类别中获取关键字的示例。希望能有所帮助

包括文件

使用Tridion.ContentManager;
使用Tridion.ContentManager.CommunicationManagement;
使用Tridion.ContentManager.Templating.Assembly;
使用Tridion.ContentManager.ContentManagement;
使用Tridion.ContentManager.ContentManagement.Fields;
使用Tridion.ContentManager.Templating;
示例代码,您可以根据需要在此处使用循环中的键和值

string catID=package.GetByName(“CategoryID”).GetAsString();
TcmUri catURI=新的TcmUri(int.Parse(catID),ItemType.Category,PubId);
var theCategory=m_Engine.GetObject(catURI)作为类别;
catKeywords=GetCatKeywords(类别);
字符串strSelect=“”;
foreach(catKeywords中的关键字k)
{
k、 Key//Keyowrd键
k、 值//关键字值
}
//关键词列表
私有IList GetCatKeywords(类别)
{
IList关键词;
如果(!Utilities.IsNull(类别))
{
过滤器=新过滤器();
filter.BaseColumns=ListBaseColumns.IdAndTitle;
关键词=category.GetKeywords(过滤器);
如果(!Utilities.IsNull(关键字))
{
返回关键字;
}
}
返回null;
}

关键字没有构造函数将字符串作为参数之一。这个字符串实际上是URI吗?如果是这样,您需要使用字符串创建一个TcmUri对象,例如TcmUri kwUri=new TcmUri(selectedKeyword)