Skip to content

Latest commit

 

History

History
185 lines (175 loc) · 8.14 KB

MediaService-Events.md

File metadata and controls

185 lines (175 loc) · 8.14 KB

#MediaService Events#

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

Usage

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);
            }
        }
    }
}

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 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.

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 use this extension method