1

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);
            }
            

        }
2
  • You'd have to get the component from the CopyEventArgs.CopiedObject, which will have the results of the copy operation.
  • The CopiedObject will be null in the Initiated event phase. Use the Processed phase instead of Initiated in the event subscription.
  • You've not called the VersionedItem.Save() method to save the metadata changes made on the copied component.
  • Rather than you processing the XML, use ItemFields available in the Tridion.ContentManager.ContentManagement.Fields namespace. It's much simpler to get/set the content and metadata fields of components based on their schema.

So in your case, this should work:

EventSystem.Subscribe<Component, CopyEventArgs>(OnCopysubject, EventPhases.Processed);
 
private void OnCopysubject(Component subject, CopyEventArgs copyEventArgs, EventPhases phase)
{
    try
    {
        Component component = (Component)copyEventArgs.CopiedObject;
        
        if (component.Metadata != null )
        {
            ItemFields metadataFields = new ItemFields(subject.Metadata, subject.MetadataSchema);

            // Based on the type of the metadata field, cast to the apppropriate class. 
            ((TextField)metadataFields["targetMarkets"]).Value = "";

            subject.Metadata = metadataFields.ToXml();
            subject.TryCheckOut();
            subject.Metadata = metadataFields.ToXml();
            subject.Save(true);
        }
    }
    catch (Exception exception)
    {

    }
}
2
  • 1
    Nice answer and good point on working with the fields over the API!
    – Alvin Reyes
    Sep 24 at 18:52
  • Thank you Sabarish, this was very useful and resolved the issue. Oct 1 at 11:49

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.