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();
}
}
}
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 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 :)
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
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.
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
It's all in here. (for future references)
https://our.umbraco.org/documentation/Reference/Events/MediaService-Events
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
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.
is working on a reply...