Posts Tagged ‘Keywords’
Blockers for deleting keywords in Tridion
Some months back I had written a custom tool to import all keywords from XML to Tridion. I thought it was a one time activity, so I didn’t care much about the tool. Completed the tool in 3 days and imported all keywords. But last week there was a change in imported keywords. That was a not major change but we had a very very tough time just to implement the changes.
Problem:
Initially we were thinking of deleting all the keywords manually and run the tool again. But there comes a difficult part, we had around 5000 keywords and at least 30% of them have been used by other items. so we couldn’t just delete manually.
After few trail runs, identified that most of the keywords
- Classified by other building blocks
- Localized in any of child
I understand that its ridiculous to do it manually. But as part of testing itself, we have partially done it manually
As I was held up in various activities, I couldn’t develop the tool immediately but thinking of developing it for future purpose.
Solutions:
Soon after I realized that there is a need for a tool, I began thinking a way of writing it. I didn’t write up anything but two approaches came into my mind immediately.
- Simple and Easy
- Get category object and retrieve all keywords in xml
- Loop though each keyword
- unclassify if classified
- unlocalize if localized
- Find root keyword and delete it.
- Nice Programming
- Get root keyword and navigate through its child recursively
- If the child is
- classified then unclassify
- localized then unlocalize
- Once the focus is back to its parent object while looping, delete the child keyword. It is something like deleting the nodes from its bottom.
I like the 2nd approach but eager to finish up the tool quickly so I’m going to try the 1st one now. But 2nd is the nice approach to do, just becoz of its algorithmic approach
Will share the source code for both approaches soon.
Create/Import child keywords in Tridion
In my earlier post, we have seen about adding keywords. After that I was trying to add child keywords in the same code sample. But things didn’t work out when it comes to add parent details. Because you can’t assign multiple parents to a keyword if you use Tridion GUI but it is possible if you use Tridion Object Model. Finally after some trial and errors, I got the working piece of code.
If you are trying to import large data into Tridion, you can modify this code using recursion. I assume that you are familiar with .Net programming so I don’t want to confuse this code sample by adding recursion. This simple code is about adding child keyword to a parent.
When you are adding the child keywords, you need to make four changes in my previous code.
1) Instantiate Category object to fetch parent key object
2) Get Parent keyword object
3) IsRoot – should be set to false as this is not a root keyword
4) Should include <tcm:ParentKeyword> within <tcm:ParentKeywords> as shown below
//initializing tridion base object
TDSE TObj = new TDSE();
//creating category object
Category catObj = (Category)TObj.GetObject(CategoryTCMURI, EnumOpenMode.OpenModeEdit, PublicationTCMURI, XMLReadFilter.XMLReadAll);
Keyword parentKey = catObj.GetKeywordByTitle("India");
//getting new Tridion keyword object
//CategoryTCMURI - TCM uri of category object
//PublicationTCMURI - TCM uri of publication
Keyword KObj = (Keyword)TObj.GetNewObject(ItemType.ItemTypeKeyword, CategoryTCMURI, PublicationTCMURI);
string title = "Chennai";
string description = "Chennai is the 4th largest metro";
string key = "Chennai";
bool isAbstract = false;
bool isRoot = false;
StringBuilder keyXML = new StringBuilder();
keyXML.Append("<tcm:Keyword xmlns:tcm='http://www.tridion.com/ContentManager/5.0'>");
keyXML.Append("<tcm:Data xmlns:tcm='http://www.tridion.com/ContentManager/5.0'>");
keyXML.AppendFormat("<tcm:Title>{0}</tcm:Title>", title);
keyXML.AppendFormat("<tcm:Description>{0}</tcm:Description>", description);
keyXML.AppendFormat("<tcm:Key>{0}</tcm:Key>", key);
keyXML.AppendFormat("<tcm:IsAbstract>{0}</tcm:IsAbstract>", isAbstract.ToString().ToLower());
keyXML.AppendFormat("<tcm:IsRoot>{0}</tcm:IsRoot>", isRoot.ToString().ToLower());
keyXML.Append("<tcm:ParentKeywords xmlns:tcm='http://www.tridion.com/ContentManager/5.0' xmlns:xlink='http://www.w3.org/1999/xlink' >");
if (parentKey != null)
{
string info = parentKey.Info.Path;
info = info.Replace(" ", "%20");
info = info.Replace("\\", @"/");
keyXML.Append("<tcm:ParentKeyword xlink:type='simple' xlink:title='" + parentKey.Title + "' xlink:href='/webdav" + parentKey.Info.Path + "/" + parentKey.Title + ".tkw' /> ");
}
keyXML.Append("</tcm:ParentKeywords>");
keyXML.Append("<tcm:RelatedKeywords xmlns:tcm='http://www.tridion.com/ContentManager/5.0' xmlns:xlink='http://www.w3.org/1999/xlink' />");
keyXML.Append("<tcm:MetadataSchema xmlns:xlink='http://www.w3.org/1999/xlink' xlink:type='simple' xlink:title='' xlink:href='tcm:0-0-0' />");
keyXML.Append("<tcm:Metadata />");
keyXML.Append("</tcm:Data>");
keyXML.Append("</tcm:Keyword>");
KObj.UpdateXML(keyXML.ToString());
KObj.Save(true);
Here’s the output
Create/Import keywords in Tridion
Last week I had a chance to work on a custom tool to create Tridion keywords. I started doing some POCs using Tridion business connecter but it failed so I tried again with Object Model (TOM). I was sure that this could be done through object model but there were no much details available in Tridion materials. We had tried various options but finally support team have helped us to find a quick way to add keywords.
Solution:
Keywords can be created/edited by updating its intermediate xml. But I don’t have a complete knowledge on this intermediate xml format as Tridion is using this for internal processing. But it worked for creating keywords.
Here’s a sample C# code to create a keyword in Tridion…
//initializing tridion base object
TDSE TObj = new TDSE();
//getting new Tridion keyword object
//CategoryTCMURI - TCM uri of category object
//PublicationTCMURI - TCM uri of publication
Keyword KObj = (Keyword)TObj.GetNewObject(ItemType.ItemTypeKeyword, CategoryTCMURI, PublicationTCMURI);
string title = "Books";
string description = "Tamil Books";
string key = "Books";
bool isAbstract = false;
bool isRoot = true;
StringBuilder keyXML = new StringBuilder();
keyXML.Append("<tcm:Keyword xmlns:tcm='http://www.tridion.com/ContentManager/5.0'>");
keyXML.Append("<tcm:Data xmlns:tcm='http://www.tridion.com/ContentManager/5.0'>");
keyXML.AppendFormat("<tcm:Title>{0}</tcm:Title>", title);
keyXML.AppendFormat("<tcm:Description>{0}</tcm:Description>", description);
keyXML.AppendFormat("<tcm:Key>{0}</tcm:Key>", key);
keyXML.AppendFormat("<tcm:IsAbstract>{0}</tcm:IsAbstract>", isAbstract.ToString().ToLower());
keyXML.AppendFormat("<tcm:IsRoot>{0}</tcm:IsRoot>", isRoot.ToString().ToLower());
keyXML.Append("<tcm:ParentKeywords xmlns:tcm='http://www.tridion.com/ContentManager/5.0' xmlns:xlink='http://www.w3.org/1999/xlink' />");
keyXML.Append("<tcm:RelatedKeywords xmlns:tcm='http://www.tridion.com/ContentManager/5.0' xmlns:xlink='http://www.w3.org/1999/xlink' />");
keyXML.Append("<tcm:MetadataSchema xmlns:xlink='http://www.w3.org/1999/xlink' xlink:type='simple' xlink:title='' xlink:href='tcm:0-0-0' />");
keyXML.Append("<tcm:Metadata />");
keyXML.Append("</tcm:Data>");
keyXML.Append("</tcm:Keyword>");
KObj.UpdateXML(keyXML.ToString());
KObj.Save(true);
Note:
Lowercase values should be assigned isRoot & isAbstract.
