How to get the keyword from category name in C# TBB?

与世无争的帅哥 提交于 2019-12-04 11:46:45

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" %>

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 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.

Perhaps you need to study the API docs some more?

vikas kumar

Af Jeremy suggested you should study API, I am providing you example of getting keywords from categories. Hope it may help

Include files

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;

Sample Code, You can use Key and Value from loop here as per your requirement.

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

"GetKeywordByTitle" is not a method on Package, its a method on Category. Can't you just new-up Keyword?

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

Cheers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!