Skip to main content

RenderedItem Metadata and Instruction

Ever wondered what the methods AddMetadata and AddInstruction do in the RenderedItem class? Did you ever noticed they were there? I’m talking about Tridion.ContentManager.Publishing.Rendering.RenderedItem and the methods:

  • public void AddMetadata(XmlElement metadata)
  • public void AddInstruction(InstructionScope scope, XmlElement instruction)

They were introduced a while back (IIRC, R5.2) and they are responsible with sending metadata from the Content Manager to the Content Delivery, or more exactly to the Deployer. This is a great idea and it’s unfortunate very few of us know about them. What’s even more unfortunate is that this is your typical Tridion half-implementation. Namely, while there is this nice API to send metadata to the Deployer, there is no API whatsoever to read it once it reaches the Deployer. You are on your own. But, let’s see what they do.

AddMetadata(XmlElement metadata)

Documentation states: "The metadata parameter can contain any valid XML structure which can be processed at the Content Delivery side. [...] When a Page with a PageTemplate is rendered this added metadata is available on the Page at the Content Delivery side. When a Component with a ComponentTemplate is rendered this added metadata is available on the Page when the ComponentPresentation is embedded, in case of a Dynamic ComponentPresenation the added metadata is available on the ComponentPresentation at the Content Delivery Side."

What is unclear is the "can be processed at the Content Delivery side". This should read, "can be processed during Deployment, in either a Deployer processor/module or in a Storage extension". This metadata is available as XML inside certain files (we'll see exactly in which ones) in the transport package and cannot be accessed after the Deployment/Publishing finishes. This metadata does not end up in any Content Delivery database tables or file system. Also, this information is only available while publishing and not for unpublish.

In the example below, I'm setting the Metadata XML node of a Folder as RenderedItem metadata. Of course, any XMLElement would do, I am simply using an easy way to get some meaningful metadata and attach it. The Folder metadata contains an Embedded Schema field "CDNSettings" that has two sub-fields: "CDN" and "UrlBasePath". The TOM.NET code looks like this:

Folder folder = engine.GetObject("tcm:14-6-2") as Folder;
engine.PublishingContext.RenderedItem.AddMetadata(folder.Metadata);

I placed the code above in a C# Fragment TBB that I added to a Dynamic Component Template.

When publishing a Page that contains a CP with that Dynamic CT, the following metadata is generated (as XML node) and it is available in the transport package in the component_presentations.xml file:

<ComponentPresentations>
...
    <RenderingMetadata>
      <Metadata xmlns="uuid:5bd95ff5-a509-4490-b37d-211ac5907c53">
      <CDNSettings>
        <CDN>Akamai</CDN>
        <UrlBasePath>http://cdn.akamai.com</UrlBasePath>
      </CDNSettings>
    </RenderingMetadata>
...

Placing the C# TBB on the Page Template will have as effect the generation of RenderingMetadata node in the transport package inside file pages.xml:

<Pages>
...
    <RenderingMetadata>
      <Metadata xmlns="uuid:5bd95ff5-a509-4490-b37d-211ac5907c53">
      <CDNSettings>
        <CDN>Akamai</CDN>
        <UrlBasePath>http://cdn.akamai.com</UrlBasePath>
      </CDNSettings>
    </RenderingMetadata>
...

AddInstruction(InstructionScope scope, XmlElement instruction)

Documentation states: "The instruction parameter can contain any valid XML structure which can be processed at the Content Delivery side. [..] Adding the instruction with a global scope is available at instruction level at the Content Delivery side. When a Page with a PageTemplate is rendered with adding a local instruction this instruction is available on the Page module at the Content Delivery side. When a Component with a ComponentTemplate is rendered with adding a local instruction this instruction available on the Page module when the ComponentPresentation is embedded, in case of a Dynamic ComponentPresenation the added local instruction is available on the ComponentPresentation module at the Content Delivery Side."

Again, the documentation is unclear regarding the "can be processed at the Content Delivery side". Same as with AddMetadata, the instruction XmlElement can be processed during deployment in either the Deployer or a Storage Extension. This information is not stored anywhere after deployment (not in file system or database based Brokers).

The InstructionScope determines where the instruction XML is available in the transport package:

  • InstructionScope.Local

Publishing using the code below will result in the RenderingInstructions node being populated with the folder metadata XML into either the component_presentations.xml or pages.xml in the transport package.

Folder folder = engine.GetObject("tcm:14-6-2") as Folder;
engine.PublishingContext.RenderedItem.AddInstruction(InstructionScope.Local, folder.Metadata);

Adding instruction in a Component Template, will create the following structure in the component_presentations.xml:

<ComponentPresentations>
...
  <ComponentPresentation IsRendered="true">
    <RenderingInstructions>
      <Metadata xmlns="uuid:5bd95ff5-a509-4490-b37d-211ac5907c53">
        <CDNSettings>
          <CDN>Akamai</CDN>
          <UrlBasePath>http://node.akamai.com</UrlBasePath>
        </CDNSettings>
      </Metadata>
    </RenderingInstructions>
...

Adding instruction in a Page Template, will create the following structure in the pages.xml:

<Pages>
...
    <RenderingInstructions>
      <Metadata xmlns="uuid:5bd95ff5-a509-4490-b37d-211ac5907c53">
        <CDNSettings>
          <CDN>Akamai</CDN>
          <UrlBasePath>http://node.akamai.com</UrlBasePath>
        </CDNSettings>
      </Metadata>
    </RenderingInstructions>
...

  • InstructionScope.Global

Using InstructionScope.Global, will generate the following XML structure in the instructions.xml file in the transport package:

<ProcessorInstructions version="6.1.0.996">
...
  <RenderingGlobalInstructions>
    <Metadata xmlns="uuid:5bd95ff5-a509-4490-b37d-211ac5907c53">
      <CDNSettings>
        <CDN>Akamai</CDN>
        <UrlBasePath>http://node.akamai.com</UrlBasePath>
      </CDNSettings>
    </Metadata>
  </RenderingGlobalInstructions>
...

Usage

On its own, this mechanism is really just a half-implementation. It is up to the implementer to read this XML on the Content Delivery side (read Deployer or Storage extension) during publishing. Remember, this won't work during unpublish, as no templates are executed (rendered) during unpublishing. What's really annoying is that the implementer is resposible with writing the code to extract the XmlElement metadata or instruction from their respective files in the transport package!

I would use this mechanism to set, via templating, information that I want to have available during deployment, knowing that this information is not going to be stored on the Content Delivery side.

Most likely I would implement a POJO or in fact a JavaBean and use JAXB to unmarshal the XML into a JavaBean. An alternative is to build an XML DOM and query it with XPath selectors.

Comments

Popular posts from this blog

Running sp_updatestats on AWS RDS database

Part of the maintenance tasks that I perform on a MSSQL Content Manager database is to run stored procedure sp_updatestats . exec sp_updatestats However, that is not supported on an AWS RDS instance. The error message below indicates that only the sa  account can perform this: Msg 15247 , Level 16 , State 1 , Procedure sp_updatestats, Line 15 [Batch Start Line 0 ] User does not have permission to perform this action. Instead there are several posts that suggest using UPDATE STATISTICS instead: https://dba.stackexchange.com/questions/145982/sp-updatestats-vs-update-statistics I stumbled upon the following post from 2008 (!!!), https://social.msdn.microsoft.com/Forums/sqlserver/en-US/186e3db0-fe37-4c31-b017-8e7c24d19697/spupdatestats-fails-to-run-with-permission-error-under-dbopriveleged-user , which describes a way to wrap the call to sp_updatestats and execute it under a different user: create procedure dbo.sp_updstats with execute as 'dbo' as

Content Delivery Monitoring in AWS with CloudWatch

This post describes a way of monitoring a Tridion 9 combined Deployer by sending the health checks into a custom metric in CloudWatch in AWS. The same approach can also be used for other Content Delivery services. Once the metric is available in CloudWatch, we can create alarms in case the service errors out or becomes unresponsive. The overall architecture is as follows: Content Delivery service sends heartbeat (or exposes HTTP endpoint) for monitoring Monitoring Agent checks heartbeat (or HTTP health check) regularly and stores health state AWS lambda function: runs regularly reads the health state from Monitoring Agent pushes custom metrics into CloudWatch I am running the Deployer ( installation docs ) and Monitoring Agent ( installation docs ) on a t2.medium EC2 instance running CentOS on which I also installed the Systems Manager Agent (SSM Agent) ( installation docs ). In my case I have a combined Deployer that I want to monitor. This consists of an Endpoint and a

Debugging a Tridion 2011 Event System

OK, so you wrote your Tridion Event System. Now it's time to debug it. I know this is a hypothetical situtation -- your code never needs any kind of debugging ;) but indulge me... Recently, Alvin Reyes ( @nivlong ) blogged about being difficult to know how exactly to debug a Tridion Event System. More exactly, the question was " What process do I attach to for debugging even system code? ". Unfortunately, there is no simple or generic answer for it. Different events are fired by different Tridion CM modules. These modules run as different programs (or services) or run inside other programs (e.g. IIS). This means that you will need to monitor (or debug) different processes, based on which events your code handles. So the usual suspects are: dllhost.exe (or dllhost3g.exe ) - running as the MTSUser is the SDL Tridion Content Manager COM+ application and it fires events on generic TOM objects (e.g. events based on Tridion.ContentManager.Extensibility.Events.CrudEven