Take the 2-minute tour ×
Tridion Stack Exchange is a question and answer site for Tridion developers and administrators. It's 100% free, no registration required.

I am working on an event to delete unused keywords, however I am running into problems determining whether or not a keyword is used for classification across any of the publications. We are using these keywords in component metadata; the field is a multi-valued Text field with values selected from a Category (Select Box).

I was hoping it would be as easy as doing something similar to below:

// OFFER_CODE_URI is the WebDav path to the Category (keywords also created here)
var category = component.Session.GetObject(OFFER_CODE_URI) as Category;
var childkeywords = category.GetKeywords().ToArray();
foreach (Keyword keyword in childkeywords)
{
    if (!keyword.HasUsingItems())
    {
        // Do some exception handling ..
        keyword.Delete();
    }
}

However, I noticed that the 'HasUsingItems()' is returning true even if the Keyword is not used for classification (but still remains unselected in the Select Box), even though I can go manually delete the keyword from the CMS. I have also tried using the method 'GetClassifiedItems()' and counting the number of classified items, however this seems to be publication specific.

if (keyword.GetClassifiedItems().Count() == 0)
{
    // Do some exception handling ..
    keyword.Delete();
}

I could loop through the necessary publications, checking to see whether or not the keyword has classified items in any of the publications and then delete it if it does not, however this could get quite expensive. I am looking for a more efficient method to determine whether or not I can delete() a keyword if it is not used for classification across any of the publications. Thanks in advance.

share|improve this question

1 Answer 1

up vote 8 down vote accepted

If the main purpose of this code is just delete keywords that are not used, you can just try to delete each keyword one by one.

If the keyword is in use, the api just won't allow you to delete if the keyword is used for classify, is used in metadata, is localized, etc... You can capture the exception and continue with the next one.

I recommend to do a quick test with a category and few keywords for testing that the scenario is correct before you run in the final environment

share|improve this answer
1  
Laziest method I ever heard of ;) But it will work. –  Chris Summers Feb 26 at 1:13
    
I know, but will be the less complex and cheapest :) –  Miguel Feb 26 at 1:46
    
Haha, thanks for the suggestion. I had thought about doing this but hoped there would be a cleaner way of doing this; but you may be right - the performance trade-off might be significant. –  Josh Hebb Feb 26 at 2:43
    
Is interesting that HasUsingItems() returns true even when there are no items using this keyword and you can delete manually. If that is the case could be something that needs to be fixed. Are you using 2011 or 2013? –  Miguel Feb 26 at 4:04
    
I agree with Miguel - WhereUsed is heavy, and with a large content set, could be 10 times slower than this 'lazy' approach. +1 for the simple and direct solution. –  robrtc Feb 26 at 4:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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