3

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));
        break;
    }
}
//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();
doc.LoadXml(componentxmlData.Content.ToString());
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);
doc.WriteTo(tx);
string str = sw.ToString();
//End here

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

componentData.Schema = new LinkToSchemaData { IdRef = schemaid };
try
{
    client.Save(componentData, null);
    MessageBox.Show("Component Created");
}
catch
{
    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">
    <items>tcm:790-202955|tcm:790-202956|tcm:790-202957|tcm:790-202958|tcm:790-202959|tcm:790-202960|tcm:790-203001-64</items>
    <successitems></successitems>
    <faileditems></faileditems>
</Content>

Item XML

<tcm:Data>
    <tcm:Title>Publishing Status</tcm:Title>
    <tcm:Type>Normal</tcm:Type>
    <tcm:Schemaxlink:type="simple"xlink:title="USSD&#xA;Publishing&#xA;Tracking"xlink:href="tcm:788-203196-8"IsMandatory="false" />
    <tcm:Content>
        <Content>
            <items>tcm:790-202955|tcm:790-202956|tcm:790-202957|tcm:790-202958|tcm:790-202959|tcm:790-202960|tcm:790-203001-64</items>
            <successitems></succesitems>
            <fialeditems></fialeditems>
        </Content>
    </tcm:Content>
    <tcm:Metadata />
    <tcm:ApprovalStatusxlink:type="simple"xlink:title="Undefined"xlink:href="tcm:0-0-0" />
    <tcm:IsBasedOnTridionWebSchema>true</tcm:IsBasedOnTridionWebSchema>
    <tcm:MultimediaTypexlink:type="simple"xlink:title=""xlink:href="tcm:0-0-0" />
</tcm:Data>
  • 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? – Andrey Marchuk Apr 14 '15 at 15:10
  • Added the source and item xml of the component – k951 Apr 15 '15 at 6:45
  • Are you sure this is the right XML? It seems that there's just one field called items – Andrey Marchuk Apr 15 '15 at 6:45
  • If you want to edit other fields, just change "items" in my answer to name of the field – Andrey Marchuk Apr 15 '15 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 – Andrey Marchuk Apr 16 '15 at 6:36
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";
content.Add(newNode);

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

        try
        {
            client.Update(componentData, null);
            MessageBox.Show("Component updated");
        }
        catch (Exception e)
        {
            MessageBox.Show("Error occured: " + e.Message);
        }
    }
| improve this answer | |
1

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);
                }
                else
                {
                    throw new Exception(string.Format("The Item '{0}' is checked out by some other user, can't be updated",
                        component.Title));
                }
            }

        }
| improve this answer | |

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.