I am trying to use the Eventsystem to update some (Metadata) Fields when a Component is copied (using copy+paste).
I do NOT get any content (or metadata) saved this way. However, I do get my new Title saved.... Any idea how I can get my Content/Metadata saved?
We do use Tridion 2011 SP1 (no HR!), could it be we need a hotfix here?
subscriptions.Add(EventSystem.Subscribe<Component, CopyEventArgs>(ComponentCopy, EventPhases.Processed));
private void ComponentCopy(Component component, CopyEventArgs args, EventPhases phase)
{
Component newComp = (Component) args.CopiedObject;
ItemFields content = new ItemFields(newComp.Content, newComp.Schema);
XhtmlField body = (XhtmlField) content["body"];
body.Value = "Er waren eens een heleboel kamelen " + DateTime.Now.ToLongDateString();
newComp.Content = content.ToXml();
ItemFields fields = new ItemFields(newComp.Metadata, newComp.Schema);
EmbeddedSchemaField embeddedField = (EmbeddedSchemaField)fields["portal"];
if (embeddedField != null)
{
ItemFields embeddedFields = embeddedField.Value;
if (embeddedFields != null)
{
TextField bla = (TextField)embeddedFields["bla"];
bla.Value = DateTime.Now.ToLongDateString();
newComp.Metadata = embeddedFields.ToXml();
}
}
// now SAVE the Component, as changes are NOT saved in this phase
try
{
if (!newComp.IsCheckedOut)
{
newComp.CheckOut();
}
newComp.Title = "TEST-" + DateTime.Now;
newComp.Save(true);
if (newComp.IsCheckedOut) newComp.CheckIn();
}
catch (Exception ex)
{
warn(string.Format("Could not save modified Component after Copy/Paste: {0}", ex.Message));
}
}