4

I'm trying to add values in the components field using core service API's.Component have embedded field with multi-value.when i am trying to add field value it is updating first field value and not adding new field. code sample is as follows:

        var schemaFields = client.ReadSchemaFields("tcm:1030-19313-8", true, new ReadOptions());

        // load a component based on that schema
        var component = (ComponentData)client.Read("tcm:1030-19314", new ReadOptions());

        //Console.WriteLine(component.Content);

        // build a magical Fields object from it
        var fields = Fields.ForContentOf(schemaFields, component);

        // let's first quickly list all values of all fields
        foreach (var field in fields)
        {
            if (field.Type == typeof(EmbeddedSchemaFieldDefinitionData))
            {
               Fields embededFields = fields[field.Name].GetSubFields();

               embededFields["SourceID"].Value = "vcvcv";
               embededFields["TargetTCMID"].Value = "vcvcv";

             }
        }

        FullVersionInfo itemInfo = null;
        itemInfo = (FullVersionInfo)component.VersionInfo;
        if (itemInfo.LockType.Value != LockType.CheckedOut)
        {
            client.CheckOut("tcm:1030-19314", true, new ReadOptions());
            component.Content = fields.ToString();
            client.Save(component, new ReadOptions());
            client.CheckIn("tcm:1030-19314", new ReadOptions());
        }

Schema is as follows:

It has only one embedded field Mapping and Mapping have two text fields SourceID,TargetTCMID

I tried AddValues() also

Actually I am new to Core Service Concept...can you please help me on this issue...

Thanks in Advance.

5

I think the AddValue() function is what you are looking for.

I didn't test this code, but I think this should help you to add new fields

foreach (var field in fields)
{
   if (field.Type == typeof(EmbeddedSchemaFieldDefinitionData))
   {
      fields[field.Name].AddValue();
      Fields embededFields = fields[field.Name].SubFields.Last();
      embededFields["SourceID"].Value = "vcvcv";
      embededFields["TargetTCMID"].Value = "vcvcv";
   }
}

This code will do the following

  • if your field is an embedded schema field, it will add a new value to the fields
  • it will then get the last value of the embedded fields (the empty one you just added)
  • it will then fill out the proper values of your empty embedded schema fields

If you need to add multiple values, just repeat this code.

| improve this answer | |
  • Great to hear. Can you set this answer as an accepted answer then. That way, it might help others who have the same problem/question – Harald Hoffelinck Apr 13 '15 at 10:22

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.