We have moved!

You are currently looking at documentation for Umbraco 8 and older versions.
Go to docs.umbraco.com/umbraco-cms/reference/notifications/mediaservice-notifications for documentation for Umbraco 9 and newer versions.

    MediaService Events

    The MediaService class implements IMediaService. It provides access to operations involving IMedia.

    Usage

    Example usage of the MediaService events:

    using System;
    using Umbraco.Core;
    using Umbraco.Core.Composing;
    using Umbraco.Core.Models.Entities;
    using Umbraco.Core.Services.Implement;
    
    namespace Umbraco8.Components
    {
        [RuntimeLevel(MinLevel = RuntimeLevel.Run)]
        public class SubscribeToMediaSavedEventComposer : ComponentComposer<SubscribeToMediaSavedEventComponent>
        {
        }
    
        public class SubscribeToMediaSavedEventComponent : IComponent
        {
            public void Initialize()
            {
                MediaService.Saved += MediaService_Saved;
            }
    
            private void MediaService_Saved(Umbraco.Core.Services.IMediaService sender, Umbraco.Core.Events.SaveEventArgs<Umbraco.Core.Models.IMedia> e)
            {
                foreach (var mediaItem in e.SavedEntities)
                {
                    if (mediaItem.ContentType.Alias == "Image")
                    {
                        //perhaps send to Azure for AI analysis of image contents or something...
                        SendToAzure(mediaItem);
                    }
                }
            }
    
            public void Terminate()
            {
                //unsubscribe during shutdown
                MediaService.Saved -= MediaService_Saved;
            }
        }
    }
    

    Events

    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:
    1. SavedEntities: Gets the collection of IMedia objects being saved.
    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
    1. SavedEntities: Gets the saved collection of IMedia objects.
    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:
    1. Entity: Gets the IMedia object being moved.
    2. ParentId: Gets the Id of the parent of the IMedia object being moved.
    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:
    1. Entity: Gets the moved IMedia object.
    2. ParentId: Gets the Id of the parent of the IMedia moved.
    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:
    1. Entity: Gets the IMedia object being trashed.
    2. ParentId: Gets the Id of the RecycleBin.
    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:
    1. Entity: Gets the trashed IMedia object.
    2. ParentId: Gets the Id of the RecycleBin.
    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:
    1. DeletedEntities: Gets the collection of IMedia objects being deleted.
    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:
    1. DeletedEntities: Gets the collection of deleted IMedia objects.
    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:
    1. Id: Gets the id of the IMedia object being deleted.
    2. DateToRetain: Gets the latest version date.
    3. SpecificVersionId: Gets the id of the IMedia object version being deleted.
    4. IsDeletingSpecificRevision: Returns true if we are deleting a specific version.
    5. DeletePriorVersions: False by default.
    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:
    1. Id: Gets the id of the deleted IMedia object.
    2. DateToRetain: Gets the latest version date.
    3. SpecificVersionId: Gets the id of the deleted IMedia version.
    4. IsDeletingSpecificRevision: Returns true if we are deleting a specific version.
    5. DeletePriorVersions: False by default.

    What happened to Creating and Created events?

    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 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. They don't trigger before and after the entity has been persisted. Instead they trigger inside the CreateMedia method which never persists the entity. It constructs a new media object.

    What do we use instead?

    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 check to see if the entity 'remembers being dirty'