I have a component with the three fields: title, intro and desc.

In the title there is some text but in the intro and desc there is no content present.

I have to pass some text to the intro and desc fields using the core service.

Below is the code which I have tried but not updating the component field

Updated the component xml

var filter = new OrganizationalItemItemsFilterData
    Recursive = false,
    ItemTypes = new ItemType[] { ItemType.Component },
    ShowNewItems = true,
    BaseColumns = ListBaseColumns.Extended
var folderId = "tcm:788-38818-2";
var componentId = "tcm:788-203197";
var xml = client.GetListXml(folderId, filter); //Hardcoded folder value
var schemaid=string.Empty;
foreach (XElement x in xml.Elements())
    string id1=x.Attribute("ID").ToString();
    if ( id1==componentId ) //Hardcoded component Id
        schemaid = x.Attribute("SchemaId").ToString();
        schemaid = schemaid.Substring(schemaid.IndexOf("=") + 2, schemaid.Length - (schemaid.IndexOf("=") + 3));
//Getting the components content and setting the new components data to default value
var componentxmlData = (ComponentData)client.Read(componentId, new ReadOptions());
var componentData = (ComponentData)client.GetDefaultData(ItemType.Component, folderId);

//Loading and rewriting the xml content starts here
XmlDocument doc = new XmlDocument();
XmlNodeList list = doc.GetElementsByTagName("Content");
foreach (XmlNode i in list)  //used to list all nodes
    i.InnerText = "Adi";
    i.InnerText = "Vijay";
//Ends here

//Conversion of XML to String starts here 
StringWriter sw = new StringWriter();
XmlTextWriter tx = new XmlTextWriter(sw);
string str = sw.ToString();
//End here

componentData.Content = str.ToString(); //with configuration 

componentData.Schema = new LinkToSchemaData { IdRef = schemaid };
    client.Save(componentData, null);
    MessageBox.Show("Component Created");
    MessageBox.Show("Try with someother name");

Xml of the component

<Content xmlns="uuid:8c6de710-3445-421d-bf97-58008a2dc0ef" xmlns:xlink="http://www.w3.org/1999/xlink">

Item XML

    <tcm:Title>Publishing Status</tcm:Title>
    <tcm:Schemaxlink:type="simple"xlink:title="USSD&#xA;Publishing&#xA;Tracking"xlink:href="tcm:788-203196-8"IsMandatory="false" />
    <tcm:Metadata />
    <tcm:ApprovalStatusxlink:type="simple"xlink:title="Undefined"xlink:href="tcm:0-0-0" />
    <tcm:MultimediaTypexlink:type="simple"xlink:title=""xlink:href="tcm:0-0-0" />
  • There's a lot of code (most of which can be shortened btw), but the main piece is missing. Could you please post XML of your component? Commented Apr 14, 2015 at 15:10
  • Added the source and item xml of the component
    – k951
    Commented Apr 15, 2015 at 6:45
  • Are you sure this is the right XML? It seems that there's just one field called items Commented Apr 15, 2015 at 6:45
  • If you want to edit other fields, just change "items" in my answer to name of the field Commented Apr 15, 2015 at 14:03
  • I've updated my answer with case when you want to add a new field. Keep in mind, however that this is generic XML management and has nothing to do with Tridion Commented Apr 16, 2015 at 6:36

2 Answers 2


Let's assume you have text field items, like in the XML you've posted and you want to change the value of this field. Here's the simplified version of what you were trying to do:

// Reading component
var componentData = (ComponentData) client.Read(componentId, new ReadOptions());

// Read schema to get namespaceUri  
var schema = (SchemaData) client.Read(componentData.Schema.IdRef, new ReadOptions());

// Pars content of a component
var content = XElement.Parse(componentxmlData.Content);
XNamespace namespace = schema.NamespaceUri;

// Find the first field node by name
var node = content.Descendands(namespace + "items").FirstNode; // There are better ways to do it
node.Value = "new value" // update the value. Keep in mind this will work for string fields, but not for links, for example

// Add a new field
var newNode = new XElement("desc");
newNode.Value = "test";

// Put content back
    componentData.Content = content.ToString();

            client.Update(componentData, null);
            MessageBox.Show("Component updated");
        catch (Exception e)
            MessageBox.Show("Error occured: " + e.Message);

You can try the below function to update a component field:

 public void UpdateComponentField(string componentId, string fieldName, string fieldValue)
             var component = (ComponentData)client.TryCheckOut(componentId, null);

                if (component.IsEditable.Value)
                    XElement xmlData;
                    XDocument doc;

                    doc = XDocument.Parse(component.Content);
                    xmlData = doc.Root;
                    var ns = xmlData.GetDefaultNamespace();
                    if (xmlData.Descendants(ns + fieldName).Any())
                        xmlData.Descendants(ns + fieldName).SingleOrDefault().Value = fieldValue;
                        component.Content = doc.ToString();

                    client.Update(component, null);
                    throw new Exception(string.Format("The Item '{0}' is checked out by some other user, can't be updated",


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.