• Sayantan Basu

Handle Hardcoded TCM ID..

Recently I have faced problem with hard coded TCM id in our current WCM project (implemented using Tridion).


We are sending the hard coded TCM id of Component Template and Category & Keyword to the delivery end. I must admit that hard coded TCM IDs all over the places is not a good idea, because if we port this thing from lower environment to higher environment, then every time we have to update the TCM IDs everywhere. I know there are several possible solutions to handle this situation. But here I will discuss one of the easiest ways.


I have discussed this situation with one of my favorite Tridion Guru Alvin Ramirez Reyes and he suggested me to create a Page template to generate the title & tcm id of all the CT and keyword dynamically for each publication. So the concept is you have to create a page template which will collect all the required information from Tridion CMS using TOM.Net and publish it to the broker database.


1) First create one TBB. 2) Crete Page template in template builder with that TBB. 3) Create a page and publish it using the page template.

First create one TBB:

I have created one .net assembly using TOM.Net to retrieve all the Component Template from the publication. Here I have implemented this for Component Template only but you can do it for any item similar way. Here are the code details i) First Define the TCM Template Title

/// <summary> /// Define TCM Template Title /// </summary> [TcmDefaultTemplate] [TcmTemplateTitle(“Dynamically Generated TCMID and Name”)]

ii) Then Implement the ITemplate interface –

class GetTemplateIdandName : ITemplate {}

iii) Now to store the Title and TCM ID of component template I have created one model class. Here is my class –

class ModelClass { public string ctTitle { get; set; } public string ctId { get; set; } public ModelClass(string _ctTitle, string _ctId) { ctTitle = _ctTitle; ctId = _ctId; } }

iv) I have also created a method to convert the output in to proper XML format –

private XmlDocument CreateXML(List<ModelClass> listComponentTemplateModel) { XmlDocument xmlDoc = new XmlDocument(); XmlNode rootNode = xmlDoc.CreateElement(“Items”); xmlDoc.AppendChild(rootNode); foreach (var item in listComponentTemplateModel) { XmlNode xmlNode = xmlDoc.CreateElement(“Item”); XmlAttribute attribute1 = xmlDoc.CreateAttribute(“ID”); attribute1.Value = item.ctId.ToString(); xmlNode.Attributes.Append(attribute1); XmlAttribute attribute2 = xmlDoc.CreateAttribute(“Title”); attribute2.Value = item.ctTitle; xmlNode.Attributes.Append(attribute2); Logger.Debug(“xmlNode: ” + xmlNode.ToString()); rootNode.AppendChild(xmlNode); } return xmlDoc; }

As per the requirement I have converted the result in XML format, but you convert it to JSON as well. v) Therefore here is the code for Transform method –

public void Transform(Engine engine, Package package) { _engine = engine; _package = package; // Create filter for component template RepositoryItemsFilter filter = new RepositoryItemsFilter(engine.GetSession()); filter.ItemTypes = new ItemType[] { ItemType.ComponentTemplate }; filter.Recursive = true; Item contentItem = package.GetByType(ContentType.Page); Page page = engine.GetObject(contentItem.GetAsSource().GetValue(“ID”)) as Page; // Reading the publication Publication pub = page.ContextRepository as Publication; List<ModelClass> _listModelClass = new List<ModelClass>(); // Getting all the CT form that particular publication foreach (ComponentTemplate item in pub.GetItems(filter)) { string title = item.Title.ToString(); string id = item.Id.ToString(); _listModelClass.Add(new ModelClass(title,id)); } XmlDocument outputXml = new XmlDocument(); // read the output in a xml format outputXml = CreateXML(_listModelClass); // Push the xml into the package as a Output. _package.PushItem(“Output”, _package.CreateXmlDocumentItem(ContentType.Xml, outputXml)); }

So here my code is ready for further use.

Crete Page template in template builder using that TBB –

Once your code is ready just build it and upload it as a TBB into the CMS. Here are my 2 TBBs – 1) .Net Assembly-


Now create a new page template using Template Builder and add your “GetTemplateandKeywordDynamically” TBB into it. (I am assuming that you are familiar with the Template Builder Tool)

Create a page and publish it using the page template

Once everything is ready just create a page in tridion and publish it using that Page Template from your publication. Here is the output of my page –

<Items> <Item ID=”tcm:87-****-32″ Title=”Accordion FAQ [4 Column]”/> <Item ID=”tcm:87-****-32″ Title=”Accordion Shop [4 Column]”/> </Items>

Thanks for reading this article. If you need the solution file or any further information for more clarification then please reply on this blog. I will mail you the entire solution.


5 views0 comments