Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8347893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:31:03+00:00 2026-06-09T07:31:03+00:00

I understand C# Code Fragments and .NET Assemblies offer the same functionality for modular

  • 0

I understand C# Code Fragments and .NET Assemblies offer the same functionality for modular template development. We manage the code fragments in the CME and assembly code in Visual Studio, but use both the same way in Template Builder.

In terms of code, I can create a C# Code Fragment Template Building Block (TBB), for example:

var timeStamp = DateTime.Now.ToString("d MMM yyyy");
package.PushItem("timeStamp from fragment", package.CreateHtmlItem(timeStamp));

I can also create a .NET assembly Template Building Block using the same code by implementing ITemplate as below.

using System;
using Tridion.ContentManager.Templating;
using Tridion.ContentManager.Templating.Assembly;

namespace CreateAndBreakTemplates
{
  [TcmTemplateTitle("Add Date to Package")]
  public class AddDateToPackage : ITemplate
  {
    public void Transform(Engine engine, Package package)
    {
      var timeStamp = DateTime.Now.ToString("d MMM yyyy");
      package.PushItem("timeStamp from assembly", 
                       package.CreateHtmlItem(timeStamp));
    }
  }
}

The docs explain that “SDL Tridion inserts the code fragment in its predefined method of a predefined class.” It looks like this class implements ITemplate and adds some references below (am I missing anything?).

The assembly setup instructions mention at least these dlls.

  • Tridion.Common.dll
  • Tridion.ContentManager.dll
  • Tridion.ContentManager.Templating.dll
  • Tridion.ContentManager.Publishing.dll

Any other difference between fragment and assembly and how would you choose between the two?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T07:31:04+00:00Added an answer on June 9, 2026 at 7:31 am

    A C# fragment is compiled into an assembly by Tridion when the template is first invoked and after it’s been modified. To compile the fragment, Tridion wraps it in some “dungeon dressing” (bonus points for those who know where that term comes from) that:

    1. Uses the Tridion.ContentManager, Tridion.ContentManager.CommunicationManagement, Tridion.ContentManager.ContentManagement and Tridion.ContentManager.Templating namespaces
    2. Makes the Package and Engine available in fields called package and engine respectively
    3. Creates a logger for the C# fragment that is available through a field called log
    4. Adds references to some commonly used assemblies (but does not add a using for their namespaces yet)

    Edit: given the other answers it seems many people are not aware of how to accomplish certain tasks in C# fragment TBBs, so I’ll document them below:

    Import additional namespaces

    To import/use additional namespaces into your C# fragment, you need to use this syntax:

    <%@ Import Namespace="Tridion.ContentManager.ContentManagement.Fields" %>
    

    Note that this will only import namespaces from assemblies that are already referenced by Tridion. There is no mechanism for you to add references to other assemblies explicitly; so if you need a third-party DLL, you will need to add it to the GAC.

    Defining custom functions

    You can define custom fields and functions in your C# fragment by using this syntax:

    <%!
    
    public static string GetDate()
    {
        return new DateTime().ToString("u").Replace(" ", "T");
    }
    
    %>
    

    Defining member fields and (nested) classes

    The syntax for defining custom functions also allows you to define nested classes and/or member fields:

    <%!
    
    public class MyLittleHelper
    {
        public MyLittleHelper(string param1)
        {
        }
    }
    
    %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Complete C newb here. Trying to learn/understand pointers by messing with simple code fragments.
i am trying to understand following fragment of javascript code <!DOCTYPE html> <html> <body>
I’m trying to understand code from http://www.yesodweb.com/book/conduits . After some fixes (like replacing Resource
I'm trying to understand code that I bought so I can modify it. In
I've been using xdebug to debug and understand code in php projects for a
I understand this code calculates the sum of the args of a variable, however,
I want to understand if code snippets are what I am looking for here.
I am trying to understand a code, what exactly &(ipA[iLower + 1] stands for
I am now trying to understand some code and I have found a pattern,
Im using Doctrine and i dont quite understand this code here: $this->hasColumn('id', 'integer', 8,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.