Skip to main content
added syntax highlighting
Source Link
Bart Koopman
  • 29.6k
  • 3
  • 40
  • 90

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;
    }
 }
@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>
}
@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")
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".

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".

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".

added another example of using custom function w/ new assumption
Source Link
Alex Klock
  • 2.9k
  • 19
  • 27

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".

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>
}

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".

Source Link
Alex Klock
  • 2.9k
  • 19
  • 27

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>
}