How can we edit one particular field of existing CMS component using Core Service API. We can parse through XML elements and update one particular field. But then this update method has to be written for each schema component I have to update. Is there an update method in API that updates one particular field leaving rest untouched?
-
Would you mind sharing the code that you wrote, which has this issue? That's really the easiest way to troubleshoot code that doesn't work as you intended.– Peter KjaerCommented Jun 3, 2016 at 8:31
-
1Hi Ritesh. Welcome to Tridion stack exchange. I'm sure there will be plenty of help available, but first of all, you'll need to work on making your question clear. Frankly - I have very little idea what you are asking. Are you wanting to update a component in the content manager? What is a COEW? What exactly have you tried already to solve your problem? Perhaps you could post your code and explain what you expect it to do, and what it does that doesn't meet your expectations. If you can clear some of these things up, you will probably get very effective help here.– Dominic CroninCommented Jun 3, 2016 at 8:34
-
I fixed a few obvious typos, hope they were indeed obvious, but please review and edit your question to make the language clear. It is fine if English is not your native language, but just spending a few seconds to review what it says before you post will be helpful to everybody.– Bart KoopmanCommented Jun 3, 2016 at 10:13
2 Answers
I agree with the comments that your question is rather vague and could use some improvement, please edit your question and add some updated info in case my answer isn't relevant.
But in the mean time, I can answer what I think your question might be. You seem to want to change a value in a Component based on other values in the Component. You mention you need to do that when something changes in the Component.
This sounds like you are in need of event handling, rather than to update the Component via the Core Service. Please take a look at the following topic in the online documentation, it describes how you can add event handlers which you can use on a Component save action. Then your code will be triggered at the right time and you can check the value of the field and update accordingly.
A long time ago I've written an example event system which contains quite a few pointers about reading and editing Component content. Please take a look at the Rapid Editorial Interface eXtension (source code included).
You can create a function like below for example, so that you don't need write it for each component type.
public void UpdateField(string componentID, string fieldName, string fieldValue, bool isMetaDataField)
{
try
{
ComponentData firstComponent = (ComponentData)tridionClient.Read(componentID, null);
XDocument doc;
XElement xmlData;
if (isMetaDataField)
{
doc = XDocument.Parse(firstComponent.Metadata);
xmlData = doc.Root;
XNamespace ns = xmlData.GetDefaultNamespace();
if (xmlData.Descendants(ns + fieldName).Count() > 0)
{
xmlData.Descendants(ns + fieldName).SingleOrDefault().Value = fieldValue;
firstComponent.Metadata = doc.ToString();
}
}
else
{
doc = XDocument.Parse(firstComponent.Content);
xmlData = doc.Root;
XNamespace ns = xmlData.GetDefaultNamespace();
if (xmlData.Descendants(ns + fieldName).Count() > 0)
{
xmlData.Descendants(ns + fieldName).SingleOrDefault().Value = fieldValue;
firstComponent.Content = doc.ToString();
}
}
try
{
tridionClient.UndoCheckOut(componentID, false, null);
}
catch (Exception ex)
{
// fail silently
}
tridionClient.Update(firstComponent, new ReadOptions());
}
catch (Exception ex)
{
throw new Exception(string.Format("[UpdateField] :: Error: '{0}'", ex.Message));
}
}
*** You may need to adjust the code, the way it's saving the component. but it works perfectly.