Copied to clipboard

Flag this post as spam?

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


  • Bo Jacobsen 604 posts 2402 karma points
    Jul 08, 2016 @ 12:17
    Bo Jacobsen
    0

    Hook into backoffice media upload event

    Hi all.

    I tried to follow this documentation on how to register custom events https://our.umbraco.org/wiki/reference/api-cheatsheet/using-iapplicationeventhandler-to-register-events

    But i cant figure how to hook into the Media event.

    This is what ive got so far. Any pointers would be appreciated.

    using System;
    using umbraco.cms.businesslogic;
    using umbraco.cms.businesslogic.media;
    using Umbraco.Core;
    
    namespace hook.event.test
    {
        public class HookIntoMedia : IApplicationEventHandler
        {
            private static readonly object LockObj = new object();
            private static bool _ran = false;
    
            public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                if (!_ran)
                {
                    lock (LockObj)
                    {
                        if (!_ran)
                        {
                            // What do i pass into Media_BeforeSave(?,?) and  Media_New(?,?)
                            Media.BeforeSave += new Media.SaveEventHandler(Media_BeforeSave);
                            Media.New() += new Media.NewEventHandler(Media_New);
                            _ran = true;
                        }
                    }
                }       
            }
    
            private void Media_BeforeSave(Media sender, PublishEventArgs e)
            {
                // Here i wanna do some crazy stuff.
                e.Cancel = true;
            }
    
            private void Media_New(Media sender, PublishEventArgs e)
            {
                // Here i wanna do some crazy stuff.
                e.Cancel = true;
            }
    
            public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                throw new NotImplementedException();
            }
    
            public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                throw new NotImplementedException();
            }
        }
    }
    
  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Jul 08, 2016 @ 19:32
    Jan Skovgaard
    100

    Hi Bo

    Unfortunately I'm no C# wizard myself but I think it's safe to say that the documentation that you refer to is outdated, which it also says in the top of the page - It's information from the old wiki, which is relevant for v4.10.x - That being said it might still work but I imagine the API's might be marked as being deprecated?

    I think you should try to see if you can find some relevant documentation for what you want to do in these pages https://our.umbraco.org/documentation/Reference/

    Information about events can be found here - https://our.umbraco.org/documentation/Reference/Events/

    The C# documentation is here https://our.umbraco.org/apidocs/csharp/ and the API documentation can be found here https://our.umbraco.org/documentation/Reference/Management/

    I know this is not a very specific answer but perhaps some of the mentioned links can lead you in the right direction? - Otherwise someone with better .NET and C# skills than me will take their time to reply too :)

    /Jan

  • Bo Jacobsen 604 posts 2402 karma points
    Jul 11, 2016 @ 10:28
  • Ian 178 posts 752 karma points
    Jul 08, 2016 @ 21:18
    Ian
    0

    as already indicated the code you have is outdated for current umbraco versions. major changes happened in version 7 this link shows an example which could form the basis of what you want to do. The key is that you use two main events ApplicationStarting and ApplicationStarted which are triggered whenever the application pool starts to register handlers for events on different services in umbraco in this case mediaservice. In addition to MediaService.Saved there is MediaService.Saving which you may sometimes want to use if you want control over what type of media is saved when uploading

  • Bo Jacobsen 604 posts 2402 karma points
    Jul 11, 2016 @ 09:05
    Bo Jacobsen
    0

    Yes i dinnt notice the documentation was outdated. Because i came from antoher document that was outdated leading to this.

    I'll take a look and then i come back ;)

    Thanks.

Please Sign in or register to post replies

Write your reply to:

Draft