Display item publication status in the Sitecore gutter

03 March 2013 at 00:00 by Ruud van Falier - Post a comment

Someone on StackOverflow asked if it is possible to display some kind of indication showing whether or not an item has been published. This is not possible by default, but I decided to quickly throw some code together and created a GutterRenderer for this.

You can find the StackOverflow question here: http://stackoverflow.com/questions/15172346/sitecore-flag-new-items-as-unpublished-items

Sitecore doesn't give us any indication of an item's publication status by default, but it's very easy to create a so called GutterRenderer to provide users with this information.
So let's build our GutterRenderer!
The class will first retrieve all the publishing targets and then check those targets for the existance of the item the gutter is being rendered for.
There are three possible results:

1) The item was not published to any of the targets
2) The item was published to at least one of the targets
3) The item was published to all the targets

The gutter will then display an icon that indicates the state of the item (1 = red, 2 = yellow, 3 = green).
When you click on the gutter icon, the Publish dialog will be shown for that item.

This is the code for our renderer:

using System.Collections.Generic;
using System.Linq;
using Sitecore;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Globalization;
using Sitecore.Shell.Applications.ContentEditor.Gutters;

namespace ParTech.Library.Gutters
{
  public class PublicationStatus : GutterRenderer
  {
    private readonly ID publishingTargetsFolderId = new ID("{D9E44555-02A6-407A-B4FC-96B9026CAADD}");
    private readonly ID targetDatabaseFieldId = new ID("{39ECFD90-55D2-49D8-B513-99D15573DE41}");

    protected override GutterIconDescriptor GetIconDescriptor(Item item)
    {
      bool existsInAll = true;
      bool existsInOne = false;

      // Find the publishing targets item folder
      Item publishingTargetsFolder = Context.ContentDatabase.GetItem(publishingTargetsFolderId);

      if (publishingTargetsFolder == null)
      {
        return null;
      }

      // Retrieve the publishing targets database names
      List publishingTargetsDatabases = publishingTargetsFolder.GetChildren()
        .Select(x => x[targetDatabaseFieldId])
        .ToList();

      // Check for item existance in publishing targets
      publishingTargetsDatabases.ForEach(delegate(string databaseName)
      {
        if (Database.GetDatabase(databaseName).GetItem(item.ID) != null)
        {
          existsInOne = true;
        }
        else
        {
          existsInAll = false;
        }
      });

      // Return descriptor with tooltip and icon
      string tooltip = Translate.Text("This item has not yet been published");
      string icon = "People/16x16/flag_red.png";

      if (existsInAll)
      {
        tooltip = Translate.Text("This item has been published to all targets");
        icon = "People/16x16/flag_green.png";
      }
      else if (existsInOne)
      {
        tooltip = Translate.Text("This item has been published to at least one target");
        icon = "People/16x16/flag_yellow.png";
      }

      return new GutterIconDescriptor()
      {
        Icon = icon,
        Tooltip = tooltip,
        Click = string.Format("item:publish(id={0})", item.ID)
      };
    }
  }
}

You need to setup the gutter in the Core database, like this:


When this is done, you can activate the gutter by right-clicking the gutter area (left side of the tree) and selecting your gutter, like this:


And here is the result of this all:


Enjoy!

BTW: The screenshots come from the development environment of our new upcoming website; a complete redesign and rebuild in Sitecore 6.6 with MVC!
Stay tuned :-)

Latest