Copied to clipboard

Flag this post as spam?

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


  • Thomas Kahn 602 posts 506 karma points
    Oct 15, 2009 @ 14:06
    Thomas Kahn
    0

    Implementing an action handler in Umbraco 4 - ActionNew() is obsolete?

    Hi!

    I've been looking at the umbraco book (http://umbraco.org/documentation/books/creating-and-using-an-action-handler) when trying to create my first action handler for Umbraco. My action handler should set umbracoNaviHide to true for a certain doctype, when a page with that type is published.

    When i work with the code in Visual Studio it says that ActionNew() is deprecated. Reading this page: http://umbraco.org/apiDocs/html/AllMembers_T_umbraco_BusinessLogic_Actions_ActionNew.htm. I quote:

    ActionNew() - Obsolete.
    A public constructor exists ONLY for backwards compatibility in regards to 3rd party add-ons. All Umbraco assemblies should use the singleton instantiation (this.Instance) When this applicatio is refactored, this constuctor should be made private.

    Does this mean that the example from the umbraco book is only for Umbraco 3? Will it work in Umbraco 4? How do I modify the example to use singleton instantiantion instead? This is not something I'm familiar with.

    Regards,
    Thomas Kahn

  • anthony hall 222 posts 536 karma points
    Oct 15, 2009 @ 15:02
    anthony hall
    1

    does this help.

       public class MyAfterPublishEvent : umbraco.BusinessLogic.ApplicationBase
        {
    
            public MyAfterPublishEvent()
            {
                Document.AfterPublish += new Document.PublishEventHandler(Document_AfterPublish);
            }
    
            static void Document_AfterPublish(Document sender, PublishEventArgs e)
            {
                if (sender.ContentType.Alias == "myAlias")
                {
                    //  
                }
    
            }
        }

     

     

  • Thomas Kahn 602 posts 506 karma points
    Oct 15, 2009 @ 15:36
    Thomas Kahn
    0

    Hi Anthony!

    Thanks for replying! I actually sorted it out myself. The code looks like this:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using umbraco.BusinessLogic;
    using umbraco.cms.businesslogic.web;

    namespace Karnhuset
    {
    public class UmbracoNaviHideToTrue : ApplicationBase
    {
    public UmbracoNaviHideToTrue()
    {
    Document.BeforePublish += new Document.PublishEventHandler(Document_SetUmbracoNaviHideToTrue);
    }

    void Document_SetUmbracoNaviHideToTrue(Document sender, umbraco.cms.businesslogic.PublishEventArgs e)
    {
    // Log the event
    Log.Add(LogTypes.Custom, sender.Id, "Document_SetUmbracoNaviHideToTrue executed in UmbracoNaviHideToTrue");

    // If it's a press release, set umbracoNaviHide to true
    if (sender.ContentType.Alias == "PressmeddelandeDoctype")
    {
    sender.getProperty("umbracoNaviHide").Value = 1;
    sender.Save();
    }
    }
    }
    }

    Correct me if I'm wrong, but I think that what was called "Action handlers" in Umbraco v3 and older was renamed "Event handlers" in Umbraco v4. Is this correct?

    /Thomas

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Oct 15, 2009 @ 16:26
    Dirk De Grave
    2

    Yup, v3 = action handlers, v4 = event handlers!

     

    Cheers,

    /Dirk

  • Thomas Kahn 602 posts 506 karma points
    Oct 16, 2009 @ 13:40
    Thomas Kahn
    0

    Thanks for sorting that out Dirk!

    I've been out of the Umbraco loop for a while and need to brush up on my knowledge of v4.

  • Comment author was deleted

    Oct 16, 2009 @ 13:44

    action handlers are still possible in v4, but v4 also has a rich event model (action handlers are limited to content, event model isn't, http://our.umbraco.org/wiki/reference/api-cheatsheet/using-applicationbase-to-register-events/overview-of-all-events )

Please Sign in or register to post replies

Write your reply to:

Draft