0

i am trying to set the default value for newly creating component through eventhandler as below

EventSystem.Subscribe<Component, LoadEventArgs>(LoadComponentData,EventPhases.TransactionCommitted);

private void LoadComponentData(Component component, LoadEventArgs args, EventPhases phases)
    {

logger.warning("component.Content" + component.Content);                               
    }

but i am getting empty value for component.Content.

what exactly i required is, Before newly created component completely loads to the author, i need to set the default value for the component.(Note: i am exlicitly saying for the newly creating component,not existing opening)

can any one help on this.

0

2 Answers 2

2

You can set default value for component fields in your schema. If for some reason you don't want to do it, you could use GetInstanceDataEventArgs event for this.

2
  • i am using same thing as you said as below EventSystem.Subscribe<Component, GetInstanceDataEventArgs>(LoadComponentData, EventPhases.Initiated); private void LoadComponentData(Component component,GetInstanceDataEventArgs args, EventPhases phases) { Logger.WriteWarning("Content:" + component.Content, ErrorCode.GENERAL); } But the event is not firing...can you give exact syntax for this..
    – Target
    Commented Apr 10, 2014 at 9:15
  • Your code seems to be fine, try other event phases and make sure you've restarted everything yo need after changing the event code Commented Apr 10, 2014 at 9:20
1

The easiest way to set default values of a componente, is by asigning default values in the schema definition, as user978511 said.

But if you want to update the values in the Event, first of all, if you want to update only the new components, you can use the boolean

args.isNewItem();

and to manage the content of the component, you can use:

ItemFields fields = new ItemFields(component.Content, component.Schema);
TextField tf = (TextField)fields["name_of_the_filed"];
tf.Value = "whatever_you_want";
component.Content = fields.ToXml();
component.Save(true);
3
  • 1
    Saving a Component in its own load event is not really a preferred or valid action I would say. I even doubt if it is already checked out here, so this code in that event is possibly going to throw an exception. Commented Apr 10, 2014 at 8:51
  • first of all i am not getting the component.content and i am not getting value in the below line ItemFields fields = new ItemFields(component.Content, component.Schema); how to get the content for this loadeventargs
    – Target
    Commented Apr 10, 2014 at 9:17
  • Yes Bart, this code is not intended for a loadComponentEvent. It was a generic example of manipulating a component content. Commented Apr 10, 2014 at 11:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.