6

I have three Schema X is content schema , Y is Embedded schema , Z is also Embedded schema. schema z is embedded within schema Y i.e also embedded schema Schema X have field called Heading

Schema Y have fields called Links and schema Z have fields called Link

using tom.net able to get the value of schema x and Y fields, Please help me out to get the value of the schema z embedded fields

sharing the code and source of the component and dwt:-

<Content xmlns="uuid:2e9e9220-fdd3-4ad0-a669-83f631bc880c">
    <Heading>Already a customer?</Heading>
    <Links>
        <Title>File a Claim</Title>
        <Link>
            <enableItemaslink>Yes</enableItemaslink>
            <Target xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="tcm:1026-62226-1024" xlink:title="New Window">New Window</Target>
            <pageUrl>tcm:424-158685-64</pageUrl>
        </Link>
    </Links>
    <Links>
        <Title>Customer Service</Title>
        <Link>
            <enableItemaslink>Yes</enableItemaslink>
            <pageUrl>tcm:424-158680-64</pageUrl>
        </Link>
    </Links>
</Content>

Code:-

var componentURI = "tcm:1042-63022";

                Component component = (Component)engine.GetObject(componentURI);
                ItemFields fields = new ItemFields(component.Content, component.Schema);
                string title = fields["Heading"].ToString();
                package.PushItem("Heading", package.CreateHtmlItem(title));
EmbeddedSchemaField links = fields["Links"] as EmbeddedSchemaField;

                if  (links !=null)
                {
                 IList<ItemFields> linkFieldValues = links.Values;
                 foreach (ItemFields innerFields in linkFieldValues)
                {
                    if (innerFields.Contains("Title"))
                    {
                        string linktitle = package.GetValue("Component.Fields.Links.Title");
                        package.PushItem("Title", package.CreateStringItem(ContentType.Text, linktitle));
                    }

                }
             }

In Dwt to render heading and title using @@Heading@@ @@Title@@

3

Here's the code illustrating your need.

var componentUri = "tcm:1042-63022";
Component component = (Component)engine.GetObject(componentUri);
ItemFields fields = new ItemFields(component.Content, component.Schema);
string heading = ((TextField)fields["Heading"]).Value;
IList<ItemFields> links = ((EmbeddedSchemaField)fields["Links"]).Values;

if (links.Count > 0)
{
    foreach (ItemFields link in links)
    {
        string title = ((TextField)link["Title"]).Value;
        ItemFields embeddedLink = ((EmbeddedSchemaField)link["Link"]).Value;
        string enableItemsAsLink = ((TextField)embeddedLink["enableItemaslink"]).Value;
        string pageUrl = ((TextField)embeddedLink["pageUrl"]).Value;

        if (embeddedLink.Contains("Target"))
        {
            Component targetComponent = ((ComponentLinkField)embeddedLink["Target"]).Value;

            // Etc.
        }
    }
}

There are some extension methods I've coded along with XView that make writing the above code more convenient and cleaner. Here's the same code as above, but using the mentioned extension methods.

var componentUri = "tcm:1042-63022";
Component component = (Component)engine.GetObject(componentUri);
string heading = component.GetText("Heading");
IList<ItemFields> links = component.GetEmbeddedFields("Links");

if (links.Count > 0)
{
    foreach (ItemFields link in links)
    {
        string title = link.GetText("Title");
        string enableItemsAsLink = link.GetText("Link/enableItemaslink");
        string pageUrl = link.GetText("Link/pageUrl");
        Component targetComponent = link.GetComponent("Link/Target");

        if (targetComponent != null)
        {
            // Do something with targetComponent.
        }
    }
}

The extension methods can be found here. http://xview.codeplex.com/SourceControl/latest#Source/XView/TridionExtensions.ItemFields.cs

4

You are nearly there. For each Links field, you have another EmbeddedSchemaField : Link

EmbeddedSchemaField links = fields["Links"] as EmbeddedSchemaField;
if  (links !=null)
  {
    // This List will contain All 'Links' embeded fields: Count = 2
    IList<ItemFields> linkFieldValues = links.Values;

    // Get one by one each Links field
    foreach (ItemFields innerFields in linkFieldValues)
       {
         // Now inside this loop, 
        // we have content of a Single 'Links' field in 'innerFields' variable
         /*  Think as if :: innerFields = <Title>
                                          <Link>
                                             <enableItemaslink>
                                             <Target>
                                             <pageUrl>
                                          </Link> 
         */
         if (innerFields.Contains("Link"))
             {
                 // get the "Link" EmbeddedSchemaField 
                 EmbeddedSchemaField Link= innerFields["Link"] as EmbeddedSchemaField ;

                 // Now 'Link' is JUST a Single Embedded Field here 
                 // so Use 'Value' property
                    ItemFields innerLinkFieldValues = Link.Value;

              /* Now think as:: innerLinkFieldValues = <enableItemaslink>
                                                       <Target>
                                                       <pageUrl>
              */ 

So you now got the inner Embedded Schema Z field: Link

Below code should not be a problem then :

// Loop over the only 3 Fields: // <enableItemaslink>,<Target>, <pageUrl>

foreach(ItemField linkField in innerLinkFieldValues )
   { 
     if (linkField.Name.Equals("enableItemaslink"))
          {
             // Assuming enableItemaslink is a Text Field
             // Else use appropriate Field Class from Tom.Net 
             TextField text = linkField as TextField;
             String value = text.Value;
           }
   }

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.