3

In order to create a new Component, I will go to the Folder level and select "Create New Component" from ribbon toolbar.

Before the Component completely loads, I want to set some default content for this Component.

I am trying through the following event handler:

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

And then:

private void LoadComponentData(Component component, LoadEventArgs args, EventPhases phases)
{
    Logger.WriteWarning("Component Title" + component.Title, ErrorCode.GENERAL);
    Logger.WriteWarning("Component Title" + component.Content, ErrorCode.GENERAL);
}

I am getting an empty value for component.Content, is there any other way to set/get the Component content for newly created Components?

What I require is, before this newly created Component completely loads to the author, I would like to set some default value for this Component. (Note: I am explicitly saying for the newly created Component, not when opening an existing one).

I can't use the Default Data from the Schema since I want to set multiple values in a multiple value field, and via the Schema I can only set a single value.

3 Answers 3

3

The Event that you're subscribing to, LoadEventArgs, is correct. However, you should subscribe to the Initiated Phase, not the TransactionCommitted Phase as you have.

Here is where you can get more info about Event Phases (login required): http://sdllivecontent.sdl.com/LiveContent/content/en-US/SDL_Tridion_2011_SPONE/concept_FCA7A7E543ED4470BFB8B697CB6F8C3C

3
  • Even in Initiated Phase also, i am not getting any content for newly creating component. i need to get the value for component.Content.
    – Target
    Commented Apr 10, 2014 at 4:03
  • I actually think the LoadEventArgs is not correct, the phase is probably irrelevant here (although I would suggest EventPhases.Processed or EventPhases.TransactionCommitted). I never tried this event on a Component, so can't be 100% sure unfortunately. Commented Apr 10, 2014 at 9:37
  • I just looked at a production piece of code we have that sets default Page Metadata Schema with some values when creating a new page. We used this: EventSystem.Subscribe<Page, LoadEventArgs>(SetPageMetadata, EventPhases.Processed); Commented Apr 10, 2014 at 10:02
1

As explained in my answer here there are a few things you have to keep in mind. You can subscribe to the LoadEventArgs and EventPhases.Processed or EventPhases.TransactionCommitted, but you have to be aware that this event can be called multiple times.

I gather you want to set default values on a newly created Component, so the first check you should do in your code is see if this is a new Component. You can do that with the condition component.Id == TcmUri.UriNull.

But you also need to see in the Environment.StackTrace through which Method you are called. Since as mentioned this event is called multiple times and the one you need is the one called by CoreServiceBase.GetDefaultData. My solution in SDL Tridion 2013 would be as follows (in 2011 it might need to be different):

[MethodImpl(MethodImplOptions.NoInlining)]
private static void LoadComponentData(Component component, LoadEventArgs args, EventPhases phases)
{
    // only react on new Components
    if (component.Id == TcmUri.UriNull) 
    {
        StackTrace stackTrace = new StackTrace();
        foreach (var frame in stackTrace.GetFrames())
        {
            // build Class.Method
            MethodBase method = frame.GetMethod();
            string name = method.ReflectedType.Name + "." + method.Name;

            // only trigger on CoreServiceBase.GetDefaultData, not on CoreServiceBase.Create (which is the second call)
            if (name.Equals("CoreServiceBase.GetDefaultData")) break;
            if (name.Equals("CoreServiceBase.Create")) return;
        }

        // add your code to set the field values here
        // note: you might need to check if the Schema of the Component is already set
        // if that is not done, then this isn't the right event to try and set default data

    }
}

A different approach could be to use the GetInstanceDataEventArgs event, that is the one called for setting the default data in the Component from its Schema.

2
  • how to set the field values here as i am getting empty value for component.Content
    – Target
    Commented Apr 10, 2014 at 9:22
  • @Target see the note in my code, you might need to check if the Schema of the Component is already set. As you can see in the answer I mentioned, I used this approach to set a Metadata Schema on a Page and fill it with default data, but for a Component I suggest you investigate the GetInstanceDataEventArgs event, if your Component doesn't have a Schema set yet in the LoadEventArgs. Commented Apr 10, 2014 at 9:25
0

You can also try using default value in the Schema for the component, if you have Static default values for the fields.

Else if there is some business logic involved to evaluate the values of the component fields. you must use the Event System to fill the fields. Please refer to the @Nickoli's answar.

also, I suggest to Re-Start the Tridion CM to take the Event System effect.

4
  • But we can set only one default value for schema field.Is there any other settings to set multiple default value.
    – Target
    Commented Apr 10, 2014 at 5:40
  • yes, default value is always one. If you want so set one value from many, follow Nick's answar. Commented Apr 10, 2014 at 5:43
  • i am getting the empty value for component content even in initiated phase also. when i can get the complete content for the component.
    – Target
    Commented Apr 10, 2014 at 5:45
  • please re-check you subscribed to the "Initiated" Phase, configured the Event Handler rightly and re-started the Tridion Content Manger. Commented Apr 10, 2014 at 5:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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