Overriding the contents of the SDL Tridion component using event handlers

How can I override the value of a component field using an event handler? When I have the code snippet below, there is no error while saving the component. But content changes made by the event handler are not reflected in the component. I expect that one value of the size field will have the value "blabla ..." as the value.

// Call to Subscribe the events EventSystem.Subscribe<Component, SaveEventArgs>(ComponentSaveInitiatedHandler, EventPhases.Initiated); private void ComponentSaveInitiatedHandler(Component component, SaveEventArgs args, EventPhases phases) { if (component.Schema.Title == "XYZ") { ItemFields Fields = new ItemFields(component.Content, component.Schema); SingleLineTextField textField = (SingleLineTextField)Fields["size"]; textField.Value = "blabla..."; } } 
+6
source share
1 answer

You need to update the Content property with an XML string as follows:

 ItemFields Fields = new ItemFields(component.Content, component.Schema); SingleLineTextField textField = (SingleLineTextField)Fields["size"]; textField.Value = "blabla..."; component.Content = Fields.ToXml(); 
+9
source

Source: https://habr.com/ru/post/920661/


All Articles