如何从 C# TBB 中的类别名称中获取关键字? [英] How to get the keyword from category name in C# TBB?

查看:18
本文介绍了如何从 C# TBB 中的类别名称中获取关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 C# TBB 获取类别中存在的关键字,以使用以下 DWT TBB 中的输出.

I am trying to fetch the keyword present in the Category using C# TBB to use the output in following DWT TBB.

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

For that I have a component with the Category field.

我正在尝试编写以下 C# TBB 来获取关键字值.

I am trying to write the following C# TBB to get the keyword value.

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

但是我遇到了以下编译错误.

But I am getting following compilation error.

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

请建议我如何实现它?

提前致谢

推荐答案

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

The error message is absolutely clear what the problem is - there's no reference to the KeywordField class. You need to import the relevant namespace:

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

同样非常清楚的是,Package 对象没有名为 GetKeywordByTitle 的方法.有一个 GetByName 方法,但这是用于从包中检索命名项,而不是从存储库中获取对象.

Also absolutely clear is the fact that the Package object doesn't have a method called GetKeywordByTitle. There is a GetByName method, but this is for retrieving a named item from the Package, not for getting an object from the respository.

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

Tridion.ContentManager.ContentManagement.Category does have a GetKeywordByTitle method, but to use this you will have to get the category first, which will likely mean having to know the URI of the category.

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

Perhaps you need to study the API docs some more?

这篇关于如何从 C# TBB 中的类别名称中获取关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆