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

Scaling Policies

This post is part of a bigger topic Autoscaling Publishers in AWS . In a previous post we talked about the Auto Scaling Groups , but we didn't go into details on the Scaling Policies. This is the purpose of this blog post. As defined earlier, the Scaling Policies define the rules according to which the group size is increased or decreased. These rules are based on instance metrics (e.g. CPU), CloudWatch custom metrics, or even CloudWatch alarms and their states and values. We defined a Scaling Policy with Steps, called 'increase_group_size', which is triggered first by the CloudWatch Alarm 'Publish_Alarm' defined earlier. Also depending on the size of the monitored CloudWatch custom metric 'Waiting for Publish', the Scaling Policy with Steps can add a difference number of instances to the group. The scaling policy sets the number of instances in group to 1 if there are between 1000 and 2000 items Waiting for Publish in the queue. It also sets the

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

Toolkit - Dynamic Content Queries

This post if part of a series about the  File System Toolkit  - a custom content delivery API for SDL Tridion. This post presents the Dynamic Content Query capability. The requirements for the Toolkit API are that it should be able to provide CustomMeta queries, pagination, and sorting -- all on the file system, without the use third party tools (database, search engines, indexers, etc). Therefore I had to implement a simple database engine and indexer -- which is described in more detail in post Writing My Own Database Engine . The querying logic does not make use of cache. This means the query logic is executed every time. When models are requested, the models are however retrieved using the ModelFactory and those are cached. Query Class This is the main class for dynamic content queries. It is the entry point into the execution logic of a query. The class takes as parameter a Criterion (presented below) which triggers the execution of query in all sub-criteria of a Criterio