SDL Tridion 2011: dynamically populate or add metadata field using C # TBB

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

The reason I want to do this is because I use DD4T, and I want the batch bundle to be automatically added to the DD4T XML file.

I 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, none of these methods work. It's impossible?

+7
source share
3 answers

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

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); } } 
+3
source

DD4T has a FieldsBuilder utility FieldsBuilder with the AddFields method, where you can add additional metadata. DD4T has a TBB that updates component metadata from folder metadata and is called InheritMetadataComponent .

You can take a look at this here, and you can 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);

+5
source

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

You can also take a look at the source Add inherited metadata to page TBB in DD4T , which also shows the way the metadata is published by the broker.

+3
source

All Articles