Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add user defined methods in C# TBB(C# code fragment)?

Tags:

c#

tridion

I am creating a C# TBB(C# Code fragment). For that I need to write a userdefined method. I tried creating it using <%! %>. How to access the user defined method in the code. Thanks in advance. Please suggest me a way to solve this issue.

like image 470
P.Muralikrishna Avatar asked May 07 '12 06:05

P.Muralikrishna


People also ask

Is there any way to create a user-defined function in C?

C allows programmers to write their own functions, also known as user-defined functions. A user-defined function has three main components that are function declarations, function definition and function call. Further functions can be called by call by value or call by reference.

How do you create a user-defined function?

You can also declare your own functions and call them within a policy. User-defined functions help you encapsulate and reuse functionality in your policy. The syntax for a function declaration is the Function keyword followed by the name of the function and a comma-separated list of runtime parameters.

How user-defined function works in C?

A function is a block of code that performs a specific task. C allows you to define functions according to your need. These functions are known as user-defined functions. For example: Suppose, you need to create a circle and color it depending upon the radius and color.

How we declare and call a user-defined function in C?

In order to transfer control to a user-defined function, we need to call it. Functions are called using their names followed by round brackets. Their arguments are passed inside the brackets. There are two types of function calls i.e, Call by value and Call by reference.


1 Answers

The TOM.NET API reference provides the following example:

<%@ Import Namespace="Tridion.ContentManager.Publishing"%>
<%!
private string ExtraString()
{
    return "Something added by the C# template";
}
%>
log.Debug("Executing C# template");
if (engine.RenderMode == RenderMode.Publish)
{
    package.GetByName(Package.OutputName).AppendToStringValue(ExtraString());
}

In addition to the above, the following syntax is supported:

<%@Import Namespace="..." %> 

Imports the namespace enclosed between the quotation marks into the code fragment. Any class you import must be present in the Global Assembly Cache.

<%! ... %> 

Declares methods, constants and classes for the rest of the code fragment to use. The declarations cannot contain the string '%>'. Note that any classes you create can only be referenced within the code fragment.

<%RunTemplate Template="tcm:1-184-2048" Class="Tridion.Templating.Examples.ExampleTemplateClass"%> 

Runs a specific .NET Assembly Template Building Block, identified by the URI in the Template attribute. This statement is typically generated by SDL Tridion 2009 itself when you upload a .NET assembly, to provide access to a specific class in the .NET Assembly.

<%@Assembly Name="System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"%> 

Inserts a reference to a nonstandard .NET assembly, which must be present in the Global Assembly Cache. Use the full assembly name.

like image 127
Nickoli Roussakov Avatar answered Nov 15 '22 06:11

Nickoli Roussakov