How To Display The Published Status Of An Item In Sitecore

Making the content editors’ lives as easy as possible should be a key priority on any project. Previously, when I have worked with large organisations, it is very difficult content publishing wise, to know who has published what. In general, I have always recommended not doing a sitewide smart-publish, but the content editor should only publish the items they are working on.

A scenario I’ve seen time and time again, is people want to publish a page made of many sub-items but they can’t remember what has been published and what hasn’t. Out of the box, Sitecore doesn’t provide this functionality, but with some custom development, we can add it in pretty easily. In today’s tutorial, I’m going to show you how to create a custom gutter item in order to achieve this. If you are new to Sitecore gutters, then I would recommend that you read, The Sitecore Gutter Explained

The Code

First up, I’ll cover the code to render the published status next to an unpublished item.

using System.Linq;
using Sitecore;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Globalization;
using Sitecore.Shell.Applications.ContentEditor.Gutters;
namespace DemoSite.Gutter
{
public class PublicationStatus : GutterRenderer
{
internal static readonly ID PublishingTargetsFolder = new ID("{D9E44555-02A6-407A-B4FC-96B9026CAADD}");
internal static readonly ID TargetDatabaseField = new ID("{39ECFD90-55D2-49D8-B513-99D15573DE41}");
public enum PublishedState
{
NotPublished,
SemiPublished,
Published
}
protected override GutterIconDescriptor GetIconDescriptor(Item item)
{
var publishingTargetsFolder = Context.ContentDatabase.GetItem(PublishingTargetsFolder);
if (publishingTargetsFolder == null)
return null;
var publishingTargetsDatabases = publishingTargetsFolder.GetChildren()
.Select(x => x[TargetDatabaseField]);
if (publishingTargetsDatabases == null)
return null;
var publishedState = PublishedState.NotPublished;
foreach (var databaseName in publishingTargetsDatabases)
{
if (Database.GetDatabase(databaseName).GetItem(item.ID) != null)
{
if (publishedState == PublishedState.NotPublished)
publishedState = PublishedState.Published;
}
else
{
if (publishedState == PublishedState.Published)
publishedState = PublishedState.SemiPublished;
}
}
// Don't display an icon for published pages
if (publishedState == PublishedState.Published)
return null;
return GutterIconDescriptorFactory(publishedState, item);
}
private GutterIconDescriptor GutterIconDescriptorFactory(PublishedState publishedState, Item item)
{
switch (publishedState)
{
case PublishedState.NotPublished:
return new GutterIconDescriptor()
{
Icon = "Applications/32x32/bullet_ball_glass_red.png",
Tooltip = Translate.Text("Not Published"),
Click = string.Format("item:publish(id={0})", item.ID)
};
break;
case PublishedState.SemiPublished:
return new GutterIconDescriptor()
{
Icon = "Applications/32x32/bullet_ball_glass_yellow.png",
Tooltip = Translate.Text("Not Published On All Targets"),
Click = string.Format("item:publish(id={0})", item.ID)
};
break;
default:
return null;
break;
}
}
}
}

This code might look a bit long, but it’s pretty simple. To make a custom Gutter, you will need to inherit from ‘GutterRenderer’. We override GetIconDescriptor() and in here add the custom logic.

First, we get all the publishing targets on the website:

var publishingTargetsFolder = Context.ContentDatabase.GetItem(PublishingTargetsFolder);
var publishingTargetsDatabases = publishingTargetsFolder.GetChildren()
.Select(x => x[TargetDatabaseField]);

Next, we check if the item has been published into the publishing database. In the majority of cases, this will just be ‘web. I’ve added an extra check in case you have more. If the item is only published in one of the publishing environments, then a yellow dot will be applied to the item, with the tooltip ‘Not Published On All Targets’.

As the code iterates through the publishing targets, if the item is found then it has been published and the PublishedState is set appropriately.

If the item is published,the code returns null and nothing needs to be rendered. If the item isn’t published, a ‘GutterIconDescriptor’ item is returned with the appropriate message. One thing to note is the ‘Click = string.Format(“item:publish(id={0})”, item.ID)’. If you click on the red dot in the content editor, the published dialog for that item will open.

Enabling the Publication Status Gutter

Go to the Sitecore Desktop and make sure the website is using the ‘Core’ database (see How To Switch Between The Core and Master Database in Sitecore).

sitecore_custom_gutter_2

Now, we are logged into the ‘Core’ database, open up the content editor

sitecore_custom_gutter_3

We will need to create a new ‘Gutter’ object that points to the custom class we have created. Navigate to:

/sitecore/templates/Sitecore Client/Content editor/Gutters/

sitecore_custom_gutter_4

To create a new gutter you just need to set two properties. I usually do a duplicate of an existing one, call it ‘Publish Status’. In the items properties, set the ‘Header’ to the value you want to appear in the Gutter Context menu and the type. This is the namespace and class name of the class that contains the custom gutter code. Note, you do not need to add the assembly name. Save the item.

Now if you go back to the Sitecore desktop and switch the database back to ‘master’. You should see your custom gutter item in the gutter context menu (right click on the left grey toolbar).

sitecore_publish_gutter_5

If you enable ‘Publication Status’ you will now see a red dot against any unpublished item and a yellow dot against semi-published items. If you click on the dot you should see the items published dialogue.

sitecore_publish_status_4

Conclusion

In today’s post I’ve covered how to add the published status next to an item in the Sitecore editor. Adding this functionality involves some custom code as it is not supported out of the box. To add in the custom functionality, we create a custom gutter item. In the ‘Core’ database we add the custom gutter via the editor, after that it’s a simple matter of enabling it in the editor.

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

More Posts

1 reply

Trackbacks & Pingbacks

  1. […] Is everything published that you think should be? I normally implement an unpublished gutter icon in my project to help troubleshoot what items are not published, reading How To Display The Published Status Of An Item In Sitecore […]

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *