Search code examples
kentico

Correct way to register a global event


I need to register a custom global event that will be executed before updating RoleInfo objects in Kentico 11 CMS.

Documentation states this:

event class.event action.event type += handler method name

Where:

Event class – event classes are containers of events related to groups of functionality
Event action – represents a specific action that occurs within the system
Event type – determines when exactly the event takes place, typically Before or After the action. Some actions only have one type: Execute

Regarding this, is the correct way to register the event I need? If not, explanation would be appreciated as this part of documentation remains unclear for me.

RoleInfoProvider.Events.Update.Before += MyMethod

Solution

  • Object events are events that can be registered for all classes that inherit from AbstractInfo base class such as UserInfo, RoleInfo, SiteInfo and more. So in your case RoleInfoProvider is not inheriting from AsbtractInfo class.

    You should use

    RoleInfo.TYPEINFO.Events.Update.Before += YourMethod
    

    You should also initialize it in your custom module class, eg.

    [assembly: RegisterModule(typeof(CompanyOverviewModule))]
    

    public class CompanyOverviewModule : Module {

    public CompanyOverviewModule()
        : base("CompanyOverview")
    {
    }
    
    // Initializes the module. Called when the application starts.
    protected override void OnInit()
    {
        base.OnInit();
    
    
       RoleInfo.TYPEINFO.Events.Update.Before += Role_UpdateBefore;
    }
    
    private void Role_UpdateBefore(object sender, ObjectEventArgs e)
    {
        RoleInfo ri = (RoleInfo)e.Object;
    
    }
    }