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:
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.
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>
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:
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.
Using InstructionScope.Global, will generate the following XML structure in the instructions.xml file 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.
- 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);
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);
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>
...
...
<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>
...
...
<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>
...
...
<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