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]);
}
}