1

I'm trying to get embedded Schema field values in a Template Building Block. The field in question is of type "link to Component" and can be present in the Component multiple times. Therefore, I need to put all the Component IDs in a list, but I plan to do that later. For the moment I have problems accessing the first field found:

string pageURI = package.GetValue("Page.ID");
Page page = (Page)engine.GetSession().GetObject(pageURI);
Component componentA = (Component)engine.GetSession().GetObject(page.ComponentPresentations[0].Component.Id);
var fieldsA = new ItemFields(componentA.Content, componentA.Schema);

// DOES NOT WORK
EmbeddedSchemaField embeddedSlideshare = (EmbeddedSchemaField)fieldsA["slideshare"];
ItemFields embeddedFields = embeddedSlideshare.Value ;

I checked the XML name of my field and there is no error

When I run it with the Template Builder, I get the following stack:

Debugging was started in process 'TcmTemplateDebugHost' with id 209180 The given key was not present in the dictionary. at System.ThrowHelper.ThrowKeyNotFoundException() at System.Collections.Generic.Dictionary`2.get_Item(TKey key) at Tridion.ContentManager.ContentManagement.Fields.ItemFields.get_Item(String fieldName) at Tridion.Templating.CSharpTemplate.CSharpSourceTemplate.Transform(Engine __engine, Package __package) at Tridion.ContentManager.Templating.Assembly.CSharpSourceCodeMediator.Transform(Engine engine, Template template, Package package) at Tridion.ContentManager.Templating.Engine.ExecuteTemplate(Template template, Package package) at Tridion.ContentManager.Templating.Engine.InvokeTemplate(Package package, TemplateInvocation templateInvocation, Template template)
at Tridion.ContentManager.Templating.Compound.CompoundTemplateMediator.Transform(Engine engine, Template templateToTransform, Package package) at Tridion.ContentManager.Templating.Engine.ExecuteTemplate(Template template, Package package) at Tridion.ContentManager.Templating.Engine.InvokeTemplate(Package package, TemplateInvocation templateInvocation, Template template)
at Tridion.ContentManager.Templating.Engine.TransformPackage(Template template, Package package) at Tridion.ContentManager.Templating.Engine.TransformItem(Template template, IdentifiableObject itemToRender) at Tridion.ContentManager.Templating.Debugging.DebuggingEngine.Run()
at Tridion.ContentManager.Templating.Debugging.DebugSession.Run()

Thanks in advance for your help

EDIT:

This is the (simplified) Schema for the Component:

<xsd:schema targetNamespace="uuid:8B3F97F1-78E6-492E-A1F5-2B312F5C1203" elementFormDefault="qualified" xmlns:tcm="http://www.tridion.com/ContentManager/5.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tcmi="http://www.tridion.com/ContentManager/5.0/Instance" xmlns="uuid:8B3F97F1-78E6-492E-A1F5-2B312F5C1203">
    <xsd:annotation>
        <xsd:appinfo>
            <tcm:Labels>
                <tcm:Label ElementName="Paragraph" Metadata="false">Paragraph</tcm:Label>
            </tcm:Labels>
        </xsd:appinfo>
    </xsd:annotation>
    <xsd:include schemaLocation="tcm:120-13617-8"/>
    <xsd:import namespace="http://www.tridion.com/ContentManager/5.0/Instance"/>
    <xsd:element name="Content">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="Paragraph" minOccurs="1" maxOccurs="unbounded" type="paragraph">
                    <xsd:annotation>
                        <xsd:appinfo>
                            <tcm:EmbeddedSchema xlink:href="tcm:120-13617-8" xlink:title="ESC_Paragraph"/>
                        </xsd:appinfo>
                    </xsd:annotation>
                </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

And the (simplified) Embedded Schema:

<xsd:schema elementFormDefault="qualified" xmlns:tcm="http://www.tridion.com/ContentManager/5.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tcmi="http://www.tridion.com/ContentManager/5.0/Instance">
    <xsd:annotation>
        <xsd:appinfo>
            <tcm:Labels>
                <tcm:Label ElementName="slideshare" Metadata="false">slideshare</tcm:Label>
            </tcm:Labels>
        </xsd:appinfo>
    </xsd:annotation>
    <xsd:import namespace="http://www.tridion.com/ContentManager/5.0/Instance"/>
    <xsd:complexType name="paragraph">
        <xsd:sequence>
            <xsd:element name="slideshare" minOccurs="0" maxOccurs="1" type="tcmi:SimpleLink">
                <xsd:annotation>
                    <xsd:appinfo>
                        <tcm:linktype>ComponentLink</tcm:linktype>
                        <tcm:AllowMultimediaLinks>false</tcm:AllowMultimediaLinks>
                        <tcm:AllowedTargetSchemas>
                            <tcm:TargetSchema xlink:href="tcm:120-48342-8" xlink:title="SC_Slideshare"/>
                        </tcm:AllowedTargetSchemas>
                    </xsd:appinfo>
                </xsd:annotation>
            </xsd:element>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

Schreenshot of the entire Schema, indicating the field in question: enter image description here

  • can you edit your question and add some information as to how your Schema and Embeddable Schema is setup, perhaps a simple snippet of the XML content of the Component as an example, so we have an idea of which fields you are trying to access? – Bart Koopman Jul 21 '16 at 12:10
  • a possible answer to your question tridion.stackexchange.com/questions/12843/… – Bart Koopman Jul 21 '16 at 12:14
  • The update is done. thank you for your help – user1178 Jul 21 '16 at 12:15
  • I simplified the embedded Schema into a single field, perhaps you can do the same for the Component Schema, since I don't exactly know which field you are embedding the Schema in. The idea of all this is to get a simple reproducible scenario without distratcions, so everybody can see what this is about and not get distracted by other detaisl with are not important. Simply posting a snippet of the Component XML would actually also be sufficient, and more readable in this case. – Bart Koopman Jul 21 '16 at 12:21
  • I just posted a photo. Hope it helps. Thank you – user1178 Jul 21 '16 at 12:37
0

Assuming your Component XML would look something like:

<Content xmlns="uuid:8B3F97F1-78E6-492E-A1F5-2B312F5C1203">
  <Paragraph>
    <slideshare xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="tcm:1-22"></slideshare>
  </Paragraph>
  <Paragraph>
    <slideshare xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="tcm:1-33"></slideshare>
  </Paragraph>
</Content>

You should be able to use the following code getting to the embedded fields;

ItemFields componentFields = new ItemFields(component.Content, component.Schema);
IList<ItemFields> paragraphs = ((EmbeddedSchemaField)componentFields["Paragraph"]).Values;

if (paragraphs.Count > 0)
{
  foreach (ItemFields embeddedFields in paragraphs)
  {
    ComponentLinkField slideshareField = (ComponentLinkField)embeddedFields["slideshare"];
    Component slideshareComponent = slideshareField.Value;
    // you now have the Component object from the link field
    // so you can use slideshareComponent.ID etc.
  }
}
  • it works perfectly. Thank you for the time you spent trying to help me – user1178 Jul 21 '16 at 13:16

Your Answer

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