When I am copy pasting the Component, the Component Metadata is not getting updated(cleaning the fields) through Event Handle, I am subscribing the event as shown below. but when checked the newly created component the metadata fields are not updated(cleaned). can anyone please help me on this.
EventSystem.Subscribe<Component, CopyEventArgs>(OnCopysubject, EventPhases.Initiated);
private void OnCopysubject(Component subject, CopyEventArgs e, EventPhases phase)
{
try
{
log.Info("ComponentMetdata.Events: " + subject.Title, LoggerCategory.General);
if (subject.Metadata != null)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(subject.Metadata.OuterXml);
var nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("uuid", xmlDoc.DocumentElement.GetAttribute("xmlns"));
var xmlNode = xmlDoc.SelectSingleNode("//uuid:metadata", nsmgr);
if (xmlNode != null)
{
foreach (XmlNode node in xmlNode)
{
if (node.Name != "targetMarkets")
{
node.InnerText = "";
}
}
XmlElement xElement = xmlDoc.DocumentElement;
subject.Metadata = xElement;
log.Info("ComponentMetdata.Events: " + "subject.Metadata.OuterXml" + subject.Metadata.OuterXml, LoggerCategory.General);
}
}
}
catch (Exception exception)
{
log.Info("ComponentMetdata.Events: " + exception, LoggerCategory.General);
}
}