You have two options:
- tap into the
<uiUpload>
pipeline
- Use media upload events
Generally speaking, I almost always recommend going with a pipeline over eventing, if the option is available. However, in this case, if there is any possibility that your media could be uploaded programmatically then I recommend that you subscribe to one or more of the following events, based on your requirements:
media:upload
media:batchupload
media:multiupload
The uiUpload
pipeline
The <uiUpload>
pipeline is a great solution if you know that your custom logic only needs to fire when a content author uploads the media. The processor fires once per content authoring action, meaning that regardless of whether the author uploads one file or five the logic will only have to run one time. This is great for performance, and means that both "multi-upload" and "single upload" are covered.
The only problem with the uiUpload
pipeline is the ui
part: it only runs when the upload is triggered from the Sitecore Client. For this reason, if there is a possibility that the media that your logic needs to run on will be uploaded programmatically then the uiUpload
pipeline is not the solution for you.
The media:upload
and related events
Sitecore's eventing model is a fantastic tool for use in a variety of circumstances. In this case, the eventing model fills in the gap left by the <uiUpload>
pipeline by providing support for the execution of custom logic when a media item is uploaded outside of the Sitecore Client.
The upload events are as follows:
media:upload
- raised when a media item is uploaded to Sitecore. The
media:multiupload
- raised when multiple media items are uploaded at once.
media:batchupload
- raised when media is uploaded via the /upload
folder on the file system
The greatest negative to the media events is their performance cost to the Sitecore Client authoring experience, as compared to the <uiUpload>
pipeline. In general, events tend to be a bit heavier than pipelines - one of, if not the worst being the item:saved
event. However, due to the fact that the media events are not repeatedly called for a single action, the performance cost for the three listed events is minimal.