Copied to clipboard

Flag this post as spam?

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


  • Paul Sørensen 302 posts 648 karma points
    Nov 30, 2009 @ 17:29
    Paul Sørensen
    0

    Calling own code when a node is created

     

    On my site I register events - that means I create nodes. Some of the data entered, when creating an event, I want to have stored in tables I have made.

    So my question is: how can I get my code called when the node is saved?

     

    Thanks Paul S

  • amunk 17 posts 32 karma points
    Nov 30, 2009 @ 18:07
    amunk
    1

    Hi Paul,

    Check out ApplicationBase for registering event.

    http://our.umbraco.org/wiki/reference/api-cheatsheet/using-applicationbase-to-register-events

    Regards,
    Anders

  • Nik Wahlberg 639 posts 1237 karma points MVP
    Nov 30, 2009 @ 18:19
    Nik Wahlberg
    1

    Hi Paul,

    you should be able to do this quite easily by extending ApplictionBase. Here's a quick example:

    using System;
    using System.Text;
    using umbraco.BusinessLogic;
    using umbraco.cms.businesslogic.web;
    using umbraco.presentation.nodeFactory;
    using umbraco.cms.businesslogic;
    using umbraco.cms.businesslogic.media;
    using System.Web;

    namespace MyNamespace
    {
        public class MyClass : ApplicationBase
        {
            StringBuilder _sb = new StringBuilder();
            Utilities _u = new Utilities();

            // default constructor; call the event
            public MyClass()
            {
                Document.AfterPublish += new Document.PublishEventHandler(Document_AfterPublish);
            }

            void Document_AfterPublish(Document sender, umbraco.cms.businesslogic.PublishEventArgs e)
            {
                //Always LOG
                Log.Add(LogTypes.Custom, sender.Id, "Event raised");

                //Check if the doctype is news
                if (sender.ContentType.Alias == "MyNodeType")
                {
                    // Just hit my node type so let's log that we did
                    Log.Add(LogTypes.Custom, sender.Id, "In my node type");
                   
                    // access document type props
                    sender.getProperty("MyDocTypeProperty");
                   
                    // execute custom code...
                   
                    Log.Add(LogTypes.Custom, sender.Id, "Success writing stuff to my custom DB");
                }
            }
        }
    }

    The most important thing to remeber is that your class is public. Then, all you need to do is drop your assembly in the /bin folder and Umbraco will pick it up.

    Hope this helps.

    -- Nik

  • Nik Wahlberg 639 posts 1237 karma points MVP
    Nov 30, 2009 @ 18:20
    Nik Wahlberg
    0

    Ooops...left some code in there that you can ignore:

    StringBuilder _sb = new StringBuilder();
    Utilities _u = new Utilities();

    Cheers.

  • Paul Sørensen 302 posts 648 karma points
    Nov 30, 2009 @ 23:22
    Paul Sørensen
    0

     

    Thanks guys

    Exactly what I was looking for

    /Paul S

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies