I set some data into ComponentTemplate using Metadata schema. And I'm trying to fetch this data through .NET code, but I can't find some related variables in engine, package to do this. Could someone help with this?

Thanks in advance.

  • What exactly have you tried? Because it is basically as simple as using the ComponentTemplate.Metadata and ComponentTemplate.MetadataSchema properties in your ItemFields collection. – Bart Koopman Apr 22 '14 at 15:10

I think you can do something like this:

var componentTemplateId =  package.GetSources("Component.ComponentTemplate")[0].GetValue("ID");
var componentTemplate = (ComponentTemplate) engine.GetSession().GetObject(componentTemplateId);
var metadata = componentTemplate.Metadata;

And example how to read a single field from Metadata:

ItemFields metadataFields = new ItemFields(componentTemplate.Metadata, componentTemplate.MetadataSchema);
TextField myTextField = (TextField)metadataFields["MyTextFieldName"];
string myTextFieldValue = myTextField.Value;

This should get you going until someone will post a simpler solution

| improve this answer | |
  • I've tried your solution. Unfortunately, package.GetSources("Component.ComponentTemplate") returns empty array. – Vitaliy Apr 22 '14 at 15:43
  • 2
    BTW, I've found the better way to get component template metadata: engine.PublishingContext.ResolvedItem.Template.Metadata – Vitaliy Apr 22 '14 at 15:59
  • You should probably check if that's not null before trying to load it, but yes, that's how I would read it too. – Nuno Linhares Apr 22 '14 at 21:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.