One of the wonderful benefits of a CMS system like Sitecore is the ability to automate what can be mind-numbing repetitive tasks.
Want to publish content when it’s approved? No problem. Use workflow.
Need to create a bunch of content items at once? Use a branch template.
Want to intercept a 404 error for an old and find the new page? Write a pipeline processor.
Want your images to automatically appear on your website when you add them to the media library? Oh wait… OK, how do we do that?
I like automating publishing, so that CMS users never need to use the “Publish” button. I believe that publishing isn’t an editorial task, and so we shouldn’t expect editors to do it. But when we do this, we’re stuck with what to do about the unversioned and otherwise unworkflowed images and PDFs that editors are adding to the media library.
Everyone always says: “There’s got to be a way to configure it to do that too.”
The good news? There is. The even better news? You do it the same way you configure other workflows.
We could take the easy way out, and schedule an automated recurring publishing job that fires every 10 minutes, or every 5 minutes, or if we’re masochists, every minute. But in my years of working with CMS systems that use publishing processes like this, I’ve learned that “there be dragons.”
One of the things I think is particularly beautiful about Sitecore’s architecture is that everything is made out of “items”. I often joke that, if you dig deep enough in the Core database, you’ll find a pair of items called “0” and “1.”
This homogeneity means that it’s easy to put a workflow on media items the same way you would a press release and to have that workflow automatically publish the media items for you.
Of course, it’s important to note that this approach as outlined here only works for items created after you set it up, and not retroactively, so do it early in a project so your users can benefit from it as soon as possible.
Also note that this approach does not work for automatically publishing media items when they change, since the default media items are unversioned, and do not move through workflow when being changed. The techniques applied here can also, however, be applied to versioned media items.
First, You Need to Create a Workflow for Media Items
This workflow should have two states (although one isn’t ever really used):
Then, the Published state should have an auto publish action below it. If you’ve created the action from scratch using the “/sitecore/templates/System/Workflow/Action” data template, you’ll want to use the following settings in it (but I’m not going to lie, I’m lazy and usually duplicate an existing one):
- Type String: Sitecore.Workflows.Simple.PublishAction, Sitecore.Kernel
- Parameters: deep=1
The newly created workflow should be configured so that the “Published” state is the default state by going to the workflow item and choosing the published state.
Also, make sure to update any other workflow transitions that might exist if you duplicated another workflow) to point to the respective state in the right workflow.
Then, Configure Files to Use the Workflow
Once you’ve created your workflow, you need to make sure that media items use it. This is done by assigning the workflow to the unversion file data template’s standard value.
The data template for files can usually be found here: /sitecore/templates/System/Media/Unversioned/File
Now, just go to the presentation tab and set the workflow you created as the default one on the standard values item.
Finally, Give it a Test
Once you’ve got it setup, you can test this by going to your media library (while in the context of the master database) and adding an image. All you should have to do is go through the normal upload procedure.
Now, switch context over to the web database (or other production database, if you have multiple configured) and look for your image. It should be right there for you. This means that your image was published behind the scenes and with no user interaction, which is exactly what we want.