Navigation

Inspect Component XML With C#

If you want to see the source XML of a Tridion component, which is more intuitive or appealing to you?
  •  <xsl:copy-of select=".">
  • component.Content.OuterXml
  • Clicking on view source tab

XSLT

I typically use three XSL functions to dig around an XML source when doing XSLT template development. When in doubt, a copy-of is GPS for XSLT.

But Now...

I'm compelled to revisit my set of templating and rending tools which have, up-until-now, focused on XSLT and the Content Delivery API.

So here's the Template Building Block Assembly version of the XSLT "Inspection" Component Template. You don't get some namespace information, but the effect is the same: "show me this component's source."

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

namespace CreateAndBreakTemplates
{
    [TcmTemplateTitle("Show XML Guts")]
    public class ShowXmlGuts : ITemplate
    {
        public void Transform(Engine engine, Package package)
        {
            Item contentItem = package.GetByType(ContentType.Component);
            Component component = engine.GetObject(contentItem.GetAsSource().GetValue("ID")) as Component;
            package.PushItem("componentSource", package.CreateHtmlItem(component.Content.OuterXml));
        }
    }
}

Okay, aside from someone pointing out I'm likely using the wrong method and incorrectly handling XML as HTML, that was rather... anticlimactic.

It's marginally better than "Hello World." Maybe we can call it the "Hello Nekkid Component Source" example for now then revisit a third option to get at component details in a future post.

No comments:

Post a Comment

Feel free to share your thoughts below.

Some HTML allowed including links such as: <a href="link">link text</a>.