3

In Tom.Net API for Tridion 2013 SP1 in Engine class for PublishingContext properties there is following remark:

The Publishing Context consists of items in the Tridion.ContentManager.Publishing assembly, and serve as context for the current publishing actions. In case no PublishingContext is available (in practice, when a Template Building Block is being debugged in the Template Builder), the property-value is null.

But when I use following code in c# code fragment

string templateID = engine.PublishingContext.RenderedItem.ResolvedItem.Template.Id.ToString();

I am able to get both Page and Component template ID in following scenarios:

  1. Publishing
  2. Tridion Preview
  3. Template Builder
  4. XPM fast-track publishing

Is is safe to use code above in order to get Template ID?

3

It is mostly safe. For ResolvedItem there is a note in the documentation that:

When used through the Template Builder, the template is not set (as it need not have been saved yet)

so you may want to check the Template property for null before trying to get it's ID.

It's worth noting that you can short-cut the RenderedItem and go directly to the ResolvedItem from the PublishingContextand that you don't need to use ToString() on the ID property of the Template, so:

var templateID = engine.PublishingContext.ResolvedItem.Template.Id;

should be adequate (it'll give you a TcmUri object, but the default method of this is ToString() anyway).

You should also consider why you need to know the template ID. If you're using it for some conditional logic, then it's not good practice to use IDs, as they can change between environments.

Update:

You mention that you wish to obtain Page Template Metadata when rendering Pages... This can be achieved in a different way. You have the Page already in the package. You can get the Tridion Page object, it's Template and the Template's metadata as follows:

var page = (Page) engine.GetObject(package.GetByName(Package.PageName));
var pageTemplate = page.PageTemplate;
if (pageTemplate.Metadata != null)
{
    var pageTemplateMetadata = new ItemFields(
        pageTemplate.Metadata,
        pageTemplate.MetadataSchema);
}
| improve this answer | |
  • thanks for quick responce. We're not using it for conditional logic, we need to access current Page Template ID in c# code fragment in order to read it's metadata content. Is our approach valid or there is some better way in order to read Page Template Metadata while Page is rendered/previewed? – Stefan Klasnic Sep 25 '15 at 13:52
  • I've updated my answer to include an alternative way of obtaining the page template metadata from the page object within the package passed to your template. – David Forster Sep 25 '15 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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