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
Publishing
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>
items