November Happy Hour will be moved to Thursday December 5th.

EventHandlers - How does it work?

Vote:
 

Hello!

I'm current trying to figure out how EventHandlers work.

This is how I think they work:

You need to implement the Interface IInitializableModule and also inherit from the class InitializableModule when creating an event.

Why do you create eventhandlers?


I think you create em for adding/changing for example a template of a pagetype in the CMS? 

If you know how eventhandlers work and if I'm wrong. Please correct me :)

#82477
Mar 13, 2014 11:19
Vote:
 

Something like this...makes it possible to react to EPiServer events like page published etc....

 

[ModuleDependency(typeof(EPiServer.Web.InitializationModule), typeof(StructureMapInitialization))]
public class PageEventsModule : IInitializableModule ....

public void Initialize(InitializationEngine context)
{

....

if (!_eventPublishingPageAttached)
{
DataFactory.Instance.PublishingPage += InstancePublishingPage;
_eventPublishingPageAttached = true;
}

...
}

public void Uninitialize(InitializationEngine context)
{
if (_eventPublishingPageAttached)
{
DataFactory.Instance.PublishingPage -= InstancePublishingPage;
}

}

 

#82482
Mar 13, 2014 12:45
Vote:
 

I get the idea @Daniel Ovaska.

So every event you create needs to inherit with IInitializableModule ?

#82483
Mar 13, 2014 12:59
Vote:
 

Ok, something like this:

1. Let's say you need to know when a page is published and deleted and send someone a mail when that happens. For a normal project you can easily add code to global.asax for application start to set up event handlers like the above. 

In Application_Start you can add 
DataFactory.Instance.PublishingPage += InstancePublishingPage;
DataFactory.Instance.DeletedPageLanguage += InstanceDeletedPageLanguage;

where InstancePublishingPage is the method you want to run when the page is published, in this case this would sent the email. Information about the page is sent in event args to the method btw. The eventhandler method has a signature like this:
private static void InstancePublishingPage(object sender, PageEventArgs e)

e.PageLink will give you a reference to the page that is being published if you need it.

So why use your own class that implements IInitializableModule and with the ModuleDependency attribute you might ask. Since you can do this easily using just global.asax. Well, sometime you don't have access to the global.asax because you are implementing an addon or similar for another site that you don't have the code to. Then you can use IInitializableModule and the attribute instead of global.asax and Application_Start. But for normal web projects, use Application_Start for this type of code instead. You might want a separate flag to make sure you never add this eventhandler more than once btw...

if (!_eventDeletedPageAttached)
{
DataFactory.Instance.DeletingPage += InstanceDeletingPage;
_eventDeletedPageAttached = true;
}

Does that answer your question?

#82488
Mar 13, 2014 14:28
Vote:
 

and no, you never need more than one IInitializableModule normally. You can add eventhandlers to all events you are interested in there...

#82490
Mar 13, 2014 14:30
Vote:
 

aha! Now I think I understand :) Thank you Daniel so much!

#82491
Mar 13, 2014 14:31
Vote:
 

aha! Now I think I understand :) Thank you Daniel so much!

#82492
Mar 13, 2014 14:31
This thread is locked and should be used for reference only. Please use the Legacy add-ons forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.