Background
I was building a feature the other day that involved overriding something in the Sitecore file watcher. I needed to set some custom behavior in the MediaCreator
to watch for a specific file being dropped into the /upload folder.
Small disclaimer: I ultimately didn’t end up using this at all, but I still learned a bit about a pipeline that I thought was kinda neat.
Discovery
The file watcher calls MediaCreator's
FileCreated
method at some point in the process. From there, it (in my case, at least) ended up calling another method in MediaCreator
called CreateFromFolder
. Somewhere a bit deeper in the call stack we get to a method called GetItemPath
which checks for the Destination
property of MediaCreatorOptions
. Destination was what I needed to modify. However, I had a bit of a problem: I couldn’t pass in my own MediaCreatorOptions
.
public virtual void FileCreated(string filePath)
{
// NOTE: Irrelevant code omitted
// .....
MediaCreatorOptions empty = MediaCreatorOptions.Empty;
empty.Build(GetMediaCreatorOptionsArgs.FileBasedContext);
this.CreateFromFolder(filePath, empty);
// .....
}
As you see, the MediaCreatorOptions.Empty
is statically passed into CreateFromFolder
. There was nowhere for me to pass in a custom one. I considered overriding this method, but there were a few others I’d have to override too, and it was starting to get a bit risky. Then I decided to take a peek at what empty.Build
did.
The Build method in MediaCreatorOptions
runs a pipeline, getMediaCreatorOptions
. Sitecore out-of-the-box doesn’t actually seem to put any processors in it, so it just relies on the default values. I was able to create a custom processor here and pipe my own logic into args, enabling me to customize the Destination
property without the need to extend the MediaCreator
at all.
Conclusion
As I mentioned earlier, I ended up not going this route anyway. It didn’t end up being practical for what I needed. Mike Reynolds did find a practical use of this pipeline, however.