SDL Tridion 2011: Dynamically fill or add a metadata field using a C# TBB
Asked Answered
P

3

7

Is it possible to set the value of a metadata field dynamically from a TBB? Or is it possible to dynamically add a metadata field that does not necessarily exist on a schema from a TBB?

The reason I want to do this is that I am using DD4T and I want to have the breadcrumb automatically added into the DD4T xml.

I have tried the following:

    public override void Transform(Engine engine, Package package)
    {
        Initialize(engine,package);

        var page = GetPage();

        string output = page.OrganizationalItem.Title;

        var parent = page.OrganizationalItem as StructureGroup;
        while (parent != null)
        {
            output = GetLinkToStructureGroupIndexPage(parent) + Separator + output;
            parent = parent.OrganizationalItem as StructureGroup;
        }

        // I tried this to dynamically add the field
        //var metadata = page.Metadata.OwnerDocument.CreateElement("breadcrumb");
        //metadata.InnerText = output;
        //page.Metadata.AppendChild(metadata);

        //I tried this to dynamically set an existing field on the schema
        foreach (XmlNode xml in page.Metadata)
        {
            Log.Debug("Metadata field:" +xml.Name);
            if(xml.Name == "breadcrumb")
            {
                xml.InnerText = output;    
            }
        }

        package.PushItem(Package.PageName, package.CreateTridionItem(ContentType.Page, page));
    }

However, neither of these methods seem to work. Is this impossible?

Pueblo answered 2/11, 2012 at 16:25 Comment(2)
I guess it depends on how DD4T reads the data. If they're reading the page/component from the package, then this should have worked. If they're actually loading it from the Engine then it will ignore any change you made to the package object.Maculate
Good point Nuno. I will look at the DD4T template source to investigate. (You probably noticed this code is based on your blog post: nunolinhares.blogspot.fr/2012/01/…, thanks for that)Pueblo
A
3

The easiest approach is to create a template class which implements DD4T.Templates.Base.BasePageTemplate. In that class, you implement the method TransformPage, which takes a DD4T page as its argument. You can access the 'TCM page' using the method GetTcmPage().

Example:

    using TCM = Tridion.ContentManager.CommunicationManagement;
    using Dynamic = DD4T.ContentModel;

    public class MyTemplate : BasePageTemplate 
    {
       protected override void TransformPage(Dynamic.Page page)
       {
          TCM.Page tcmPage = GetTcmPage();
          string breadCrumbs = GetBreadCrumbs (tcmPage); // TODO: implement GetBreadCrumbs
          Field f = new Field();
          f.Name = "breadCrumbs";
          f.FieldType = FieldType.Text;
          f.Values.Add(breadCrumbs);
          page.MetadataFields.Add("breadCrumbs", f);
       }
    }
Acord answered 3/11, 2012 at 22:21 Comment(0)
C
5

DD4T has utility class FieldsBuilder with AddFields method where you can inject additional metadata. DD4T has a TBB which does update component metadata from Folder Metadata and it is called InheritMetadataComponent.

You could take a look at this here and you could implement the same:

http://code.google.com/p/dynamic-delivery-4-tridion/source/browse/trunk/dotnet/DD4T.Templates/InheritMetadataComponent.cs

FieldsBuilder.AddFields(component.MetadataFields, tcmFields, 1, false, mergeAction, Manager);

Consummation answered 2/11, 2012 at 16:43 Comment(0)
T
3

page.MetadataFields.Add(name, field); should work if your template extends the DD4T.Templates.Base.BasePageTemplate

You can also take a look at the source of the Add inherited metadata to page TBB in DD4T, that also shows a way of adding Metadata which gets published to the broker.

Tayyebeb answered 2/11, 2012 at 16:49 Comment(0)
A
3

The easiest approach is to create a template class which implements DD4T.Templates.Base.BasePageTemplate. In that class, you implement the method TransformPage, which takes a DD4T page as its argument. You can access the 'TCM page' using the method GetTcmPage().

Example:

    using TCM = Tridion.ContentManager.CommunicationManagement;
    using Dynamic = DD4T.ContentModel;

    public class MyTemplate : BasePageTemplate 
    {
       protected override void TransformPage(Dynamic.Page page)
       {
          TCM.Page tcmPage = GetTcmPage();
          string breadCrumbs = GetBreadCrumbs (tcmPage); // TODO: implement GetBreadCrumbs
          Field f = new Field();
          f.Name = "breadCrumbs";
          f.FieldType = FieldType.Text;
          f.Values.Add(breadCrumbs);
          page.MetadataFields.Add("breadCrumbs", f);
       }
    }
Acord answered 3/11, 2012 at 22:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.