5

I have Tcm id of category in my app.config. In my C# TBB i am writing below code to get all keywords associated with this category

public class GetKeywords : ITemplate
  {
    public void Transform(Engine engine, Package package)
      {
        string categoryID =ConfigurationManager.AppSettings["catgoryTcmUri"];

        Category cat = (Category)engine.GetObject(categoryID);

        XmlDocument keywordsXml = new XmlDocument();
        keywordsXml.LoadXml(cat.GetListKeywords().OuterXml);
        Item output = package.CreateXmlDocumentItem(ContentType.Xml, keywordsXml);
        package.PushItem("CategoryKeywords", output);
    }
  }

I am getting follwing error in Template Builder:

 Object reference not set to an instance of an object.
 at Tieto.Gadgets.Templating.GetKeywords.Transform(Engine engine, Package package)
 at Tridion.ContentManager.Templating.Assembly.AssemblyMediator.Transform(Engine engine, Template template, Package package)
 at Tridion.ContentManager.Templating.Assembly.CSharpSourceCodeMediator.RunTemplate(Engine engine, Package package, String templateUri, String className)
 at Tridion.Templating.CSharpTemplate.CSharpSourceTemplate.Transform(Engine __engine, Package __package)
 at Tridion.ContentManager.Templating.Assembly.CSharpSourceCodeMediator.Transform(Engine engine, Template template, Package package)
 at Tridion.ContentManager.Templating.Engine.ExecuteTemplate(Template template, Package package)
 at Tridion.ContentManager.Templating.Engine.InvokeTemplate(Package package, TemplateInvocation templateInvocation, Template template)
 at Tridion.ContentManager.Templating.Compound.CompoundTemplateMediator.Transform(Engine engine, Template templateToTransform, Package package)
 at Tridion.ContentManager.Templating.Engine.ExecuteTemplate(Template template, Package package)
 at Tridion.ContentManager.Templating.Engine.InvokeTemplate(Package package, TemplateInvocation templateInvocation, Template template)
 at Tridion.ContentManager.Templating.Engine.TransformItem(Template template, IdentifiableObject itemToRender)
 at Tridion.ContentManager.Templating.Debugging.DebuggingEngine.Run()
 at Tridion.ContentManager.Templating.Debugging.DebugSession.Run()
2
  • Did you check this already? stackoverflow.com/questions/12908803/…
    – Shiva
    Commented Dec 17, 2015 at 21:11
  • 3
    What is this app.config? is it the config file of your application running at the Content Delivery side or is it some application config which you are trying include and use in the C# TBB itself? Commented Dec 18, 2015 at 4:55

2 Answers 2

5

Here are few things you should cross check as the code looks fine.

2
  • i was relying on the app.config ,that is my templating solution application configuration file. And that is why categoryID is getting null. i removed it from app.config and assigned it directly in my code : string categoryID = "tcm:2033-3222-512"; code is working fine now,i am getting all keyqords in a xml. Thanks @PankajGaur for this clue – just one query now, what if i want to add some configuration in my templating solution, where i need to place my app.config in tridion. Commented Dec 18, 2015 at 8:27
  • 1
    Store it in some kind of configuration component, parameterise your template building block with a parameters schema, create the ID in your package with a C# building block, store it in an XML file in a known location on the file system... You choose! Commented Dec 18, 2015 at 9:22
4

Use Template Builder to debug you code by adding TemplatingLogger lines between your commands.

public class GetKeywords : ITemplate
  {

    TemplatingLogger log = TemplatingLogger.GetLogger(this.GetType());

    public void Transform(Engine engine, Package package)
      {
        string categoryID = ConfigurationManager.AppSettings["catgoryTcmUri"];

        log.Debug("categoryID: " + categoryID)

        Category cat = (Category)engine.GetObject(categoryID);

        XmlDocument keywordsXml = new XmlDocument();
        keywordsXml.LoadXml(cat.GetListKeywords().OuterXml);
        Item output = package.CreateXmlDocumentItem(ContentType.Xml, keywordsXml);
        package.PushItem("CategoryKeywords", output);
    }
  }

When you run your Component Template or Page Template that has the C# TBB your are mentioning you can exactly localize the line that is raising an error. When you know the line this is 90% of your solution.

PS. I can't be sure but probably categoryID is in you case empty

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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