#MediaService Events#
The MediaService class implements IMediaService. It provides easy access to operations involving IMedia.
Example usage of the MediaService events:
using Umbraco.Core;
using Umbraco.Core.Events;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
namespace My.Namespace
{
public class MyEventHandler : ApplicationEventHandler
{
public MyEventHandler()
{
MediaService.Saved += MediaServiceSaved;
}
void MediaServiceSaved(IMediaService sender, SaveEventArgs<IMedia> e)
{
foreach (var mediaItem in e.SavedEntities)
{
UploadToAzure(mediaItem);
}
}
}
}
Event | Signature | Description |
---|---|---|
Saving | (IMediaService sender, SaveEventArgs<IMedia> e) |
Raised when MediaService.Save is called in the API. NOTE: It can be skipped completely if the parameter "raiseEvents" is set to false during the Save method call (true by default). "sender" will be the current IMediaService object. "e" will provide:
|
Saved | (IMediaService sender, SaveEventArgs<IMedia> e) |
Raised when MediaService.Save is called in the API and after data has been persisted. NOTE: It can be skipped completely if the parameter "raiseEvents" is set to false during the Save method call (true by default). "sender" will be the current IMediaService object. "e" will provide: NOTE: See here on how to determine if the entity is brand new
|
Moving | (IMediaService sender, MoveEventArgs<IMedia> e) |
Raised when MediaService.Move is called in the API. NOTE: If the target parent is the Recycle bin, this event is never fired. Try the Trashing event instead. "sender" will be the current IMediaService object. "e" will provide:
|
Moved | (IMediaService sender, MoveEventArgs<IMedia> e) |
Raised when MediaService.Move is called in the API. The event is fired after the content object has been moved. NOTE: If the target parent is the Recycle bin, this event is never fired. Try the Trashed event instead. "sender" will be the current IMediaService object. "e" will provide:
|
Trashing | (IMediaService sender, MoveEventArgs<IMedia> e) |
Raised when MediaService.MoveToRecycleBin is called in the API. "sender" will be the current IMediaService object. "e" will provide:
|
Trashed | (IMediaService sender, MoveEventArgs<IMedia> e) |
Raised when MediaService.MoveToRecycleBin is called in the API. "sender" will be the current IMediaService object. "e" will provide:
|
Deleting | (IMediaService sender, DeleteEventArgs<IMedia> e) |
Raised when MediaService.DeleteMediaOfType, MediaService.Delete, MediaService.EmptyRecycleBin are called in the API. "sender" will be the current IMediaService object. "e" will provide:
|
Deleted | (IMediaService sender, DeleteEventArgs<IMedia> e) |
Raised when MediaService.Delete, MediaService.EmptyRecycleBin are called in the API. "sender" will be the current IMediaService object. "e" will provide:
|
DeletingVersions | (IMediaService sender, DeleteRevisionsEventArgs e) |
Raised when MediaService.DeleteVersion, MediaService.DeleteVersions are called in the API. "sender" will be the current IMediaService object. "e" will provide:
|
DeletedVersions | (IMediaService sender, DeleteRevisionsEventArgs e) |
Raised when MediaService.DeleteVersion, MediaService.DeleteVersions are called in the API. "sender" will be the current IMediaService object. "e" will provide:
|
Both the MediaService.Creating and MediaService.Created events have been obsoleted. Why? Because these events are not guaranteed to trigger and therefore should not be used. This is because these events only trigger when the MediaService.CreateMedia method is used which is an entirely optional way to create media entities. It is also possible to simply construct a new media item - which is generally the preferred and consistent way - and therefore the Creating/Created events will not execute when constructing media that way. Further more, there's no reason to listen for the Creating/Created events because they are misleading since they don't actually trigger before and after the entity has been persisted, they simply trigger inside the CreateMedia method which never actually persists the entity, it simply just constructs a new media object.
The MediaService.Saving and MediaService.Saved events will always trigger before and after an entity has been persisted. You can determine if an entity is brand new in either of those events. In the Saving event - before the entity is persisted - you can check the entity's HasIdentity property which will be 'false' if it is brand new. In the Saved event you can use this extension method