2

I want some help accessing the categories and keywords from event code. The below code worked in Tridion 2009 event code, but i am unable to get some classes in new event code.

Keyword keyword = null;
Keywords parentKeywords = null; //not available in Tridion 2011
TDSEClass class2 = null;
Category category = null;

EventHelper.WriteEventToHTMLLog("Getting all the Case study keywords that tagged the component");

//ListColumnFilter is an enum with the Tridion.ContentManager.Interop.TDSDefines
//(compCurrent.GetListKeywords is not available in Tridion 2011
XDocument document =     XDocument.Parse(compCurrent.GetListKeywords(ListColumnFilter.XMLListExtended).Replace("tcm:Item", "tcmItem"));

foreach (XElement element in document.Descendants("tcmItem"))
{
    string uRI = (element.Attribute("ID") != null) ? element.Attribute("ID").Value.ToString() : string.Empty;
    string str9 = (element.Attribute("OrgItemID") != null) ? element.Attribute("OrgItemID").Value.ToString() : string.Empty;
    category = (Category)class2.GetObject(str9, EnumOpenMode.OpenModeView,    class2.GetPublication(compCurrent.Publication.ID), XMLReadFilter.XMLReadAll);
    EventHelper.WriteEventToHTMLLog("Found Category with name: " + category.Title);
    keyword = (Keyword)class2.GetObject(uRI, EnumOpenMode.OpenModeView,     class2.GetPublication(compCurrent.Publication.ID), XMLReadFilter.XMLReadAll);

    parentKeywords = keyword.ParentKeywords;
    EventHelper.WriteEventToHTMLLog("keyword parent found");
    if (parentKeywords != null)
    {
        foreach (Keyword keyword2 in parentKeywords)
        {
            EventHelper.WriteEventToHTMLLog("kword.Title" + keyword2.Title);
            title = keyword2.Title;
        }
    }
}

Keywords class is unavailable in new event code. Please help.

  • 1
    I have to admit I have problems reading through this code... could you explain what you're trying to do instead of only posting the code? And yes, of course there is a Keyword class in 2011+. – Nuno Linhares Mar 12 '14 at 14:32
  • 1
    In addition to what Nuno has said, it would be great if you state what exactly you want to access or do – Pankaj Gaur Mar 12 '14 at 14:50
  • @Nuno, I was trying to acess the keyword which are taggged to the component. I wanted to access the keyword title.. – Varun Doharey Mar 13 '14 at 6:02
3

Like Nuno mentioned, I am not quite sure what your code is supposed to do. However, keywords can be accessed in 2011 via the following:

// TcmUri (itemId, itemType, pubId)
TcmUri categoryTcmUri = new TcmUri(5021, ItemType.Category, 45);
var category = component.Session.GetObject(categoryTcmUri) as Category;
var kws = category.GetKeywords();
foreach(Keyword kw in kws)
{
     // do something with the keyword
}

While my answer is quite general, I am not entirely sure what you are looking to do with the keywords. If you could clarify I am sure someone would be glad to help you out if this doesn't answer your question.

| improve this answer | |
  • ,, thanks i was looking to access keyword, i will try this and get back if any issues. – Varun Doharey Mar 13 '14 at 6:04
2

Here's an example of how I would read all Keywords linked to a component (taking into account multi-value Keyword fields, and EmbeddedSchema fields.

using System;
using System.Collections.Generic;
using Tridion.ContentManager;
using Tridion.ContentManager.ContentManagement;
using Tridion.ContentManager.ContentManagement.Fields;
using Tridion.ContentManager.Extensibility;
using Tridion.ContentManager.Extensibility.Events;

namespace ReadKeywordsFromComponents
{
    [TcmExtension("Do Something")]
    public class DoSomething : TcmExtension, IDisposable
    {
        private readonly List<EventSubscription> _subscriptions = 
            new List<EventSubscription>();

        public DoSomething()
        {
            SubscribeEvents();
        }

        private void SubscribeEvents()
        {
            EventSubscription subscription = EventSystem.Subscribe<Component, 
                SaveEventArgs>(DoSomethingOnSave, EventPhases.Initiated);
            _subscriptions.Add(subscription);
        }

        private void DoSomethingOnSave(Component component, 
                                       SaveEventArgs args, 
                                       EventPhases phases)
        {
            Dictionary<TcmUri, Keyword> keywords = new Dictionary<Keyword>();
            if (component.ComponentType == ComponentType.Normal)
            {
                ItemFields content = new ItemFields(component.Content, component.Schema);
                GetKeywordsFromFields(content, keywords);
            }
        }


        private static void GetKeywordsFromFields(IEnumerable<ItemField> content,
                                                  Dictionary<TcmUri, Keyword> keywords)
        {
            foreach (ItemField field in content)
            {
                if (field is KeywordField)
                {
                    KeywordField keyField = (KeywordField) field;
                    foreach (Keyword keyword in keyField.Values)
                    {
                        if (!keywords.ContainsKey(keyword.Id))
                        {
                            keywords.Add(keyword.Id, keyword);
                            // If you only need the title, you could do it like this.
                            string title = keyword.Title;
                        }
                    }
                }
                if (field is EmbeddedSchemaField)
                {
                    EmbeddedSchemaField embeddedSchema = (EmbeddedSchemaField) field;
                    foreach (ItemFields fields in embeddedSchema.Values)
                    {
                        GetKeywordsFromFields(fields, keywords);
                    }
                }
            }
        }

        public void Dispose()
        {
            foreach (EventSubscription subscription in _subscriptions)
            {
                subscription.Unsubscribe();
            }
        }
    }
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.