Reinoud van Dalen

February 17, 2014

Creating an Unpublish command for Sitecore

So 2 things that clients ask at least once are:

  • How can I see if an item is published?
  • How can I unpublish an item?

I will answer both questions and will show the way for one...

How can I see if an item is published?

I usually check the web database and see if it’s there and in what state, but for the client that is not an option so there is no real way to check other than go and look on the website.

So I went and looked around if any other developers have found an elegant solution and it just so happens that PartechIT has already done so and even blogged about it. You can find the blog and details here:

For my work I have used this as inspiration for my final solution which shows the 3 states in the gutter:

  • Green flag: item is published
  • Yellow flag: item is published, but changes are not
  • Red flag: item is not published

A simple but effective solution if you ask me. I find myself a bit lost in a project without this feature because I've gotten used to it.

How can I unpublish an item?

Maybe the real question should be: Is there a real reason not to enable clients to unpublish? Please leave a comment with any thoughts about this.

For now we assume there is no real objection to the unpublish button so the first thing we do is add the button in the core database, right next to the publish button: /sitecore/content/Applications/Content Editor/Ribbons/Chunks/Publish/Unpublish (Template: Large Button). Make sure you give it an Header, Icon and set the Click value to “item:unpublish”.

Next we create an UnpublishCommand class, inheriting from Sitecore’s Command class and implement the Execute method like so:

Some description

The next thing we do is parse the parameters and get confirmation that we do actually want to unpublish and if yes then we iterate through known targetdatabases and delete the item if present.

Some description

And now all we need to do is add the item:unpublish command to the Sitecore configuration and we are good to go!

Some description

We definetely should add the option to just unpublish one language or all and we could add QueryState methods and disable the unpublish button if you don’t want it to or maybe even when you see it isn’t published, but for now this will do fine I think.

TAGS: sitecore content editor