3

We are using Core service to create components in Tridion using the Fields class. Component schema has one Multi Value Component Link field and one Single Value Component Link Field. We are reading the input TCM Id's for component link field from excel file.

While creating the XmlElement for single value component link field it adds the namespace and href attribute to XmlElement, but while creating XmlElements for multivalue component link fields it does not add namespace and href attribute to XmlElement.

Below is the code snippet:

foreach (ItemFieldDefinitionData field in schemaFieldsData.Fields)
                         {                                

if (String.IsNullOrEmpty(excelTable.Rows[i][field.Name].ToString()))
                                continue;
                            if (field.MaxOccurs == 1)
                            {
                                if (field.GetType() == typeof(MultimediaLinkFieldDefinitionData))
                                {
                                    var defData = (MultimediaLinkFieldDefinitionData)field;
                                    string multimediaSchemaId = defData.AllowedTargetSchemas[0].IdRef;
                                    //Handle the Multimedia Component link field
                                    ComponentData data = getMultimediaContentDetails(excelTable.Rows[i][field.Name].ToString(), multimediaFilesPath, multimediaFolderId, multimediaSchemaId);
                                    fields[field.Name].Value = data.Id;
                                }
                                else if (field.GetType() == typeof(XhtmlFieldDefinitionData))
                                    fields[field.Name].Value = "<div xmlns='http://www.w3.org/1999/xhtml'>" + excelTable.Rows[i][field.Name] + "</div>";
                                else
                                    fields[field.Name].Value = excelTable.Rows[i][field.Name].ToString();
                            }
                            else
                            {
                                string[] multiValues = excelTable.Rows[i][field.Name].ToString().Split('$');
                                for (int j = 0; j < multiValues.Length; j++)
                                {
                                    if (field.GetType() == typeof(MultimediaLinkFieldDefinitionData))
                                    {
                                        var defData = (MultimediaLinkFieldDefinitionData)field;
                                        string multimediaSchemaId = defData.AllowedTargetSchemas[0].IdRef;
                                        //Handle the Multimedia Component link field
                                        ComponentData data = getMultimediaContentDetails(multiValues[j], multimediaFilesPath, multimediaFolderId, multimediaSchemaId);
                                        fields[field.Name].AddValue(data.Id);
                                    }
                                    else if (field.GetType() == typeof(XhtmlFieldDefinitionData))
                                        fields[field.Name].AddValue("<div xmlns='http://www.w3.org/1999/xhtml'>" + multiValues[j] + "</div>");
                                    else if (field.GetType() == typeof (ComponentLinkFieldDefinitionData))
                                        fields[field.Name].AddValue(multiValues[j]);
                                    else
                                        fields[field.Name].AddValue(multiValues[j]);
                                }
                            }
1

On further digging, we found that multivalue component links are actually not multiple components in a collection field but parallel component link with same xml name. the following change in code started creating multivalue component links

 else if (field.GetType() == typeof (ComponentLinkFieldDefinitionData))
{
fields.AddFieldElement(new ItemFieldDefinitionData()
                                                    {

                                      Description = field.Description,
                                        Name = field.Name,

                                                    });

                               fields[field.Name].Values[j] = multiValues[j];

                                                }

adding this generated content xml in following format

  <Content xmlns="uuid:7b92ff60-c3fa-49cc-9b67-acbc0c9a5916">
    <Links d2p1:href="tcm:8-5599" d2p1:type="simple" xmlns:d2p1="http://www.w3.org/1999/xlink" />
    <Links d2p1:href="tcm:8-5599" d2p1:type="simple" xmlns:d2p1="http://www.w3.org/1999/xlink" />
    <Title>sdfsdfsd</Title>
    <CssStyling>dsfd</CssStyling>
  </Content>

Though it was creating component, I was not able to understand the meaning of namespace d2p1 and hence I manually replaced d2p1 with xlink to match it with default component content

  static string ChangeNamespace(XNamespace nameSpace,string xml)
        {
            xml = xml.Replace("d2p1", "xlink");

            return xml;


        }

Not fully satisfied with the solution but it is working.

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.