3

Edit following request for clarity: I wanted to know if it were possible to get the Schema of an embedded field directly using the razor mediator. As the answers below state, it isn't.


Original Question: Is it possible to get the schema title of an embedded schema within a razor template?

I have this razor template which is looping over a repeatable embedded schema.

@foreach(dynamic comp in Component.Fields.links)
{
    {
        <p>@comp.Schema.Title</p>
    } 
}

@comp.Schema.Title is giving the following error in template builder:

DynamicItemFields: Key 'schema' Not Found In ItemFields

  • 'comp' is not a component but a field inside this loop which makes the question slightly misleading imo... – Bjørn van Dommelen May 14 '13 at 12:20
9

You won't be able to get the embedded schema directly with the Razor Mediator, but since its just a wrapper of the TOM.NET API, you can create your own function that returns the schema's title.

The following example allows you to pass it a ComponentModel instance and a name of the field that you want to get the embedded schema's title for:

@using Tridion.ContentManager.ContentManagement.Fields

@functions {
    public string GetEmbeddedSchemaTitle(Models.ComponentModel component, string fieldName) {
        ItemFields fields = new ItemFields(component.TridionObject.Content, component.Schema);
        EmbeddedSchemaField embeddedField = fields[fieldName] as EmbeddedSchemaField;
        if (embeddedField != null) {
            return ((EmbeddedSchemaFieldDefinition)embeddedField.Definition)
                .EmbeddedSchema.Title;
        }
        return null;
    }
 }

Then, for you first example, you could do the following (assuming your field "link" is a multi-valued component link field, and the field name using an embedded schema for each of these components is "MyEmbeddedField"):

@foreach (dynamic comp in Component.Fields.link) {
    <div>Title For "MyEmbeddedField": @GetEmbeddedSchemaTitle(comp, "MyEmbeddedField")</div>
}

Update w/ New Assumption

Looking at the error message you provided, the above use example's assumptions are probably incorrect. It looks like your "link" field is a multi-valued embedded schema field, in which case a use example for that would be:

GetEmbeddedSchemaTitle(Component, "link")

Notice that you are not passing your "comp" variable in, but the actual "Component" variable for the assumption that Component contains an embedded schema field named "link".

3

If you look at the Component's XML you will find no trace of this embedded schema, as Tridion embeds it in the owning component.

The XML will look +/- like this:

<Content>
    <Field>value</Field>
    <EmbeddedField>
        <Field>Value</Field>
    </EmbeddedField>
</Content>

There's no property in the component itself indicating that this contains an embedded schema. I don't know about Razor, my impression is that you can use the full TOM.NET from it anyway. With TOM.NET you could do something like this:

ItemFields fields = new ItemFields(Component.Content, Component.Schema);
EmbeddedSchemaField embedded = (EmbeddedSchemaField)fields["EmbeddedField"];
Schema schema = embedded.Definition.EmbeddedSchema;

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.