Creating User Defined Classes in C# Code Fragment

Let’s see how we write classes in a C# Code fragment ( C# TBB ) and how these classes behave when we save/invoke  the TBB.

Let’s get movin’…

Classes are defined inside a C# Code Fragment  using the construct: <%! %>.

This USER defined class is placed as NESTED CLASS of the PREDEFINED CLASS created by Tridion when compiling the C# Code fragment. Check this blog post for in depth details on C# Code Fragment compilation and the Predefined classes , methods generated by tridion together with understanding the relation of  Tridion’s Predefined class with User defined Class in a C# Code Fragment

That said above, what we need to understand is that the User defined classes in a C# Code Fragment cannot access the predefined variables like: log, engine,  and package , since these variables are actually declared Private in the Predefined class  generated during compilation.

In short all the rules of a Nested Class vs Outer Class follows.

[ You can simply Copy and paste below code in a C# Code Fragment and run using Template Builder to see the output ]

// MUST pass the engine, package variables to our custom class in case we want to use 
// the engine, package's functionality within the Custom Class
TridionLearner learner = new TridionLearner(engine, package);


// a custom function inside C# Code Fragment
public void PushWelcomeMessageToPackage()
  package.PushItem("Function", package.CreateStringItem(ContentType.Text, "Got It"));

// A custom Class inside C# Code Fragment
 public class TridionLearner 
    private Package _package {get;set;}
    private Engine _engine {get;set;}
           public TridionLearner(Engine engine, Package package)
                   this._engine = engine;
                   this._package = package;
          public void DisplayMessage()
                                   "Hi There"));






  1. Inside our Custom Class, we CANNOT access the engine, package, log variables of Predefined class generated by Tridion, as these variables are Private.
  2. In case we need to use engine, package variables in our Custom Class, Simply Pass the original engine, package variables in context as shown in Line :
    TridionLearner learner = new TridionLearner(engine, package);

    This will simply make the engine, package variables available to be used in our own class.

Hope you enjoyed this post. Comments and Suggestions are welcome.

2 thoughts on “Creating User Defined Classes in C# Code Fragment

  1. Thanks a lot Nitesh for sharing such a concept. I have myself seen many tridion people ignoring or underrating the power of C# TBBs. Personally from my own experience , i have created a lot C# TBBs and with concepts on creating Function and Classes now in TBB, it has for sure got the capability of Assemblies or any full fledged programs written to actually process items from content manager. and make a lot more utilities , data and dynamically generated HTML available in Package item.



  2. Very well explained topic of C# TBB. One useful way I have utilized this is to set properties of classes and then push them to Package by overriding the ToString() method to add data in a format. This helped me a lot to simplify some of my complex data processings in Templates.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s