String Writer

my tech stuffs…

Archive for July 2011

Iterate all Inner Exceptions in C#

with 2 comments

In simple applications or pages, handling an exception will not be a difficult task. But when your application is using any web services or threading, most of the time, actual exception details will be stored in InnerException of the main Exception object. In such cases, we need to iterate all the inner exceptions to get the actual error message.

Here is a code snippet to iterate all inner exceptions from the root Exception object.


private List<string> GetAllErrMessages(Exception ex)
{
   List<string> messages = new List<string>();
   //assign the current exception as first object and then loop through its
   //inner exceptions till they are null
   for(Exception eCurrent = ex.Exception; eCurrent != null; eCurrent = eCurrent.InnerException)
   {
      messages.Add(eCurrent.Message);
   }
}

My team said they never saw any code like handling Exception objects using for loop. I don’t know but I learnt this from my lead when I was working on Adrenalin HRMS product. Just sharing the code for your review ;)

Written by visvabalaji

July 26, 2011 at 4:43 pm

Posted in C#

Tagged with , ,

Tridion .Net Template Building Blocks (package) – 2

with one comment

As part of previous post, this will just explain about package object in .Net Templating.

Just to understand what is package in the context of Tridion…During publishing Content Manager provides the publishable contents to Content Distributor. That is the publisher packages all the contents, metadata and instructions for publishing and deploying, in the form of XML. The transport service will send this package to a receiver in the Content Deployer and the Content Deployer will process the content according to the instruction given in the package. (Can refer the process here)

This package can be accessed through .Net templating(TBB) using C# fragments or .Net assemblies. Actually TBB will be executed in the context of given package. In order to customize the contents/instructions, package (typeof Package) object must be edited during templating process. TBB retrieves the relevant items from the package and store the modified contents back into the package object.

Both .Net templating approaches transforms the package object.

Using C# fragments

C# fragments will be a part of Transform(…) in a predefined class so the package variable is accessible by default.

Using .Net Assemblies

A class must inherit ITemplate interface in order to make it as TBB, which will have Transform(…) as part of interface implementation.

public class SampleTemplate : ITemplate
{

   public void Transform(Engine engine, Package package)
   {

   }
}

Execute using Template Builder

TBB can be executed using Template Builder, which is an add-on tool in Tridion. After including TBB for compilation, Default Finish Actions will be included as last TBB. During this process, the package contains only the contents before processing Default Finish Actions; and after Default Finish Actions has been executed, all the publishable contents will be stored in the Output of package object.

package object:

This is a stack type of object and gives access to the contents of the package being processed by the current template (Component or Page). Any ContentType items can be pushed in or popped out from the package.

Package class is defined in Tridion.ContentManager.Templating namespace.

When the package is being processed against Component Template, the package will contain Component Item, through which the Component details can be accessed.

Example,

package.PushItem("Title", package.CreateStringItem(ContentType.Text, package.EvaluateExpression("Component.Title")));

In case of Page Template, the package will contain Page Item.

Example,

package.PushItem("Title", package.CreateStringItem(ContentType.Text, package.EvaluateExpression("Page.Title")));

Finally the Output Item of Component or Page Template must contain the contents to publish.

Package variables

To access a value in the package, the following string format should be used.

[Item name].([Item name]).[value selector]

Example,

To access Component title => Component.Title

To access CreatedBy from metadata => Component.MetaData.CreatedBy

Here’s some predefined/reserved item names in Templating which are constants of Package class.

Names

Details

Example

Component Contains the component currently being processed Component.Title
Components Contains the list of components associated with the page currently being processed
Page Contains the page currently being processed Page.Title
ComponentTemplate Contains the current component template being processed against the Component
Field Contains the Field of the Component that is being processed Component.MetaData.CreatedBy
Output Contains the content to publish, in XML format
TemplateRepeatIndex Used to looping through the collections in a Dreamweaver template <!– TemplateBeginRepeat name=”Fields.locations” –>${RenderComponentField(“Fields.locations”, TemplateRepeatIndex)}<!– TemplateEndRepeat –>

Some frequently used methods in package:

PushItem(…) – Items will be added in the package using this method, LIFO (Last In, First Out) approach. Each item should be defined with a name in order to insert it in the package but multiple items can have same name.

package.PushItem("PageTitle", package.CreateStringItem(ContentType.Text, package.EvaluateExpression("Page.Title")));

GetByType(…) – Retrieves an item based the specific content type

Item item = package.GetByType(ContentType.Component);

GetAllByType(…) – Retrieves all items based on the specific content type

Items items = package.GetAllByType(ContentType.Component);

GetByName(…) – Retrieves an item by its name

package.GetByName(Package.OutputName);

GetValue(…) – Retrieves the value of an item

Component component = engine.GetObject(package.GetValue("Component.ID")) as Component;

Remove(…) – Removes an item from the package

Item componentsItem = package.GetByName(Package.ComponentsName);
package.Remove(componentsItem); //it will remove only the first item if many items used same name

CreateStringItem(…) – creates string type of an item

package.PushItem("PageTitle", package.CreateStringItem(ContentType.Text, “Sample Page")));

CreateHtmlItem(…) – creates html type of an item

String marketUrl = package.EvaluateExpression("Components.Fields.MarketUrl");
package.PushItem("MarketLink", package.CreateHtmlItem(marketUrl));

EvaluateExpression(…) – evaluates an expression (predefined variables or user defined)

package.PushItem(package.CreateStringItem(ContentType.Text, package.EvaluateExpression("3>=3"))); //user defined
package.PushItem("PageTitle", package.CreateStringItem(ContentType.Text, package.EvaluateExpression("Page.Title"))); //predefined variables

Written by visvabalaji

July 11, 2011 at 11:58 pm

Tridion .Net Template Building Blocks – 1

leave a comment »

During my internal training to freshers, most of the folks raised questions about template building blocks. I’m glad that they understood Tridion basics quickly and done few hands-ons successfully. But I had to organize another quick discussion to clarify their doubts on TBBs and I’m just documenting our discussions in this blog.

We are yet to get Tridion 2011 access so these are all based on Tridion 2009 :(

Generally SDL Tridion templating helps to transform the content from Content Manager and publish them to target location (or presentation server).

Few points about Template Building Blocks (TBB)

  • It transforms the content from Content Manager into publishable content to target location (or presentation server), in other words, it just transforms the content from XML to (X)HTML.
  • Template building blocks are inserted in a component template (or) page template in an order.
  • Collections of building blocks can be grouped together to form a Compound Template using the Template Builder.
  • Each TBB can be used in multiple Compound Templates.

Tridion supports following .Net template building blocks,

  1. C# fragments
  2. .Net assembly

Here are few details about these types,

C# fragments

We can simply write C# code fragments in the Content Manager Explorer interface (in the Source tab of TBB). These code snippets will be a part of ITemplate.Transform(…) in a predefined class. Hence package, engine and log objects are accessible without any additional reference and all the items in the Content Manager can be accessible through C# code.

Usual practice is to use when you are writing small junk of codes on a package.

Limitations:

  • Cannot create classes as these codes are already part of another class.
  • Cannot debug the code as we are writing in Content Manager Explorer interface.

.NET assembly

This is the typical way of creating C# class library projects which will return dll file. Here a class should implement ITemplate interface to create a TBB. If there are more classes that implements ITemplate, then TBB will be created for each classes.


using Tridion.ContentManager.ContentManagement;
using Tridion.ContentManager.ContentManagement.Fields;
using Tridion.ContentManager.Publishing;
using Tridion.ContentManager.Templating;
using Tridion.ContentManager.Templating.Assembly;

namespace TridionSamples
{
  public class SampleTemplate : ITemplate
  {

     /// <summary>
     /// Transform as defined by ITemplate.
     /// </summary>
     /// <param name="engine">Templating engine</param>
     /// <param name="package">Package to process</param>
     public void Transform(Engine engine, Package package)
     {

     }
  }
}

If we are making further changes in the .Net assembly, the latest dll can be uploaded to Tridion Content Manager using TCMUploadAssembly.exe command-line utility. Hope this can be posted later.

In both cases, the following objects are important to write TBBs

  • package – contains the content to be published.
  • engine – gives you the access to Content Manager in the current context.
  • log – writes log messages through template logger.

I believe these 3 objects should be detailed, to understand better.

Written by visvabalaji

July 9, 2011 at 2:15 pm

Posted in Tridion

Tagged with , ,

SDL Tridion Classroom sessions

with 2 comments

Before I started working on Tridion projects, almost nobody in my DC was aware of Tridion CMS. When we got a project from one of our client, we didn’t want to miss that opportunity. So we had to arrange a professional training from SDL Tridion. Those two weeks sessions was good enough to get started on our project.

Now I’m on the verge of signing another Tridion project and also we are expecting few more opportunities. We realized that its good time to train others and, of course its always good to have additional hands. So we randomly selected 10 peoples from a bunch of .Net developers and discussed with them on their interest in learning Tridion. When everything was set, we prepared a schedule for two weeks. Theories in the first week, 2 hours daily, and lab sessions in the second week.

This is our second week so team is fully involved in doing lab exercises. I can say that people are more interested in learning Tridion than we thought. Good to know that bunch of Tridion developers are coming out. Now its all rely on getting into right projects :)

Since we are concentrating primarily on technical front, we focused only on core topics.

Topics Details
Basics of Content Management Systems & SDL Tridion 1) Introduction of CMS, WCM, ECM
2) Basics of SDL Tridion
3) Demo on creating building blocks
Template Building Blocks 1) Basics of TBB
2) Template Builder
3) Demo of creating TBBs in all types
Event Systems 1) Basics of Tridion Events
2) Customizing Event Systems
3) Deploying, Enabling and Debugging Event Systems
Workflows 1) Basics of Tridion Workflows
2) Linking workflows with Tridion
3) Services related to Workflows
Content Broker 1) Basics of Content Broker 
2) Dynamic Publishing

 

This schedule was based on SDL Tridion 2009 and we are trying to setup a lab on Tridion 2011, which would definitely need another week session ;)

Written by visvabalaji

July 8, 2011 at 3:19 am

Posted in Tridion, WCM

Tagged with , ,

Follow

Get every new post delivered to your Inbox.