Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • prinzie 13 posts 42 karma points
    Jul 28, 2014 @ 13:36
    prinzie
    0

    EventHandler Document_AfterPublish

    How do you register a Document_AfterPublish in Umbraco 7. 'Normaly' this works, but it does not in umbraco 7. What am I doing wrong? It does log 'OnApplicationStarting 1' etc just never enters Document_AfterPublish

    using Umbraco.Core;

    using umbraco.cms.businesslogic;

    using GeneralHelper;

    using umbraco.BusinessLogic;

    using umbraco.cms.businesslogic.web;

     

    namespace EventHandlers

    {

        public class RegisterEvents : IApplicationEventHandler

        {

            public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)

            {

            }

     

            public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)

            {

              Log.Add(LogTypes.Notify, 0, "OnApplicationStarting 1");

              Document.AfterPublish += Document_AfterPublish;

              Log.Add(LogTypes.Notify, 0, "OnApplicationStarting 2");

            }

     

            public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)

            {

                Log.Add(LogTypes.Notify, 0, "RegisterEvents");

            }

     

            public void Document_AfterPublish(Document sender, PublishEventArgs e)

            {

                Log.Add(LogTypes.Notify, 0, "Document_AfterPublish");

                GeneralHelper.IndexNews.IndexAllNewsNow();

            }

        }

    }

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Jul 28, 2014 @ 14:03
    Stefan Kip
    0

    Please read this: http://our.umbraco.org/documentation/Reference/Events/application-startup
    It should explain what to use :-)

  • prinzie 13 posts 42 karma points
    Jul 30, 2014 @ 13:43
    prinzie
    0

    Thank you. I tried several og the samples, but none of them seem to be working in Umbraco 7.1.4 - It never enters the 'AfterPublish' or 'BeforePublish' when i hit the save & publish or save botton. 

    The only way I got Umbraco 7 to react on something when hitting the save & publish, was by adding my own method in umbraco.controllers.js, but there must be a better way to do it.

        $scope.saveAndPublish = function () {

            var message = performSave({ saveMethod: contentResource.publish, statusMessage: "Publishing..." });

            MYCODE();

            return message;

        };

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Jul 30, 2014 @ 17:01
    Stefan Kip
    101

    Use this:

    public class UmbracoApplicationEventHandler : ApplicationEventHandler
    {
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            base.ApplicationStarted(umbracoApplication, applicationContext);
    
            ContentService.Published += ContentService_Published;
        }
    
        void ContentService_Published(global::Umbraco.Core.Publishing.IPublishingStrategy sender, PublishEventArgs<IContent> e)
        {
            // Do stuff
        }
    }
    

    More information: http://our.umbraco.org/documentation/Reference/Events-v6/ContentService-Events

  • prinzie 13 posts 42 karma points
    Jul 31, 2014 @ 13:45
    prinzie
    0

    Thank you! Now it's working.

  • Russell McGinnis 48 posts 183 karma points
    Jul 31, 2014 @ 18:59
    Russell McGinnis
    0

    You know I hope people are going to say "there are no dumb questions" when they read this so here goes:

    Lets say I define a class called "UmbracApplicationEventHandler" and I derive it from "UmbracoEventHandler" as above. How does an instance of this class get created and therefore any event defined within it called ?

    I ask because I have done this, in order to register a 404 handler using the IContentFinder interface, but the "ApplicationStarting" event never gets called.

    Thanks

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Jul 31, 2014 @ 19:05
    Stefan Kip
    0

    First of all: you should derive from "ApplicationEventHandler".
    Second: the class is created because of the Umbraco system scanning for classes deriving from ApplicationEventHandler.

  • Russell McGinnis 48 posts 183 karma points
    Jul 31, 2014 @ 19:08
    Russell McGinnis
    0

    Thanks, appreciate the information. The "UmbraceEventHandler" was a slip as I am indeed deriving from "ApplicationEventHandler".

Please Sign in or register to post replies

Write your reply to:

Draft