Episerver 6 - hook into the Publish event

Vote:
 

Hi,

Using Episerver 6 R2 is it possible to hook into the Publish event? 

Im thinking of using the following code:

 [InitializableModule]
    [ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
    public class EventsInitialization : IInitializableModule
    {
  

        public void Initialize(InitializationEngine context)
        {
            

        }

}

But im not sure what i need to add to get the Publish event.

Thanks

Jon

#194856
Jul 04, 2018 16:31
Vote:
 

Hi Jon,

You've got a couple of options for which event to tie in to, depending on whether you want to perform an action before or after the publish has happened.

To hook in to the publish event you'll need one of the following:

DataFactory.Instance.PublishedPage += MyPagePublishedHandler;
DataFactory.Instance.PublishingPage += MyPagePublishingHandler;

Then your event handler would look something like this:

private void MyPagePublishingHandler(object sender, PageEventArgs e)
{
    //Do stuff here
}

One word of caution though - As I recall (and my memory's awful so this may not be true), initialization modules weren't available in Episerver 6 so you may need to attach the event handler in the Application_Start event either in global.asax or in a custom httpmodule.

#194875
Jul 05, 2018 12:07
Vote:
 

Hi,

Thanks for this.

I did manage to get this to work on Episerver 6 R2. I thought Id let you know as im sure ill be looking for this code again :)

 [InitializableModule]
    [ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
    public class EventsInitialization : IInitializableModule
    {

        public void Initialize(InitializationEngine context)
        {
            context.InitComplete += InitComplete;

        }

        private void InitComplete(object sender, EventArgs e)
        {
            EPiServer.DataFactory.Instance.PublishedPage += PublishedContent;
        }

        public void Preload(string[] parameters)
        {
        }

        public void Uninitialize(InitializationEngine context)
        {
        }

        private void PublishedContent(object sender, PageEventArgs e)
        {
            //DO STUFF HERE
        }

}

[Pasting files is not allowed]

#194876
Jul 05, 2018 12:12
* 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.