How to flag new items as unpublished items?

喜你入骨 提交于 2019-12-03 14:18:40
Ruud van Falier

Never thought about this, but it's actually pretty easy to fix.

I created a GutterRenderer that indicates wether an item has been published to at least one, to all, or to none of the publishing targets.

EDIT: Added Click behaviour. When you click the gutter icon, the Publish dialog will be shown for that item.

First I will show you the code that I wrote for this and then I'll show you screenshots of the setup and the result.

Here is the code:

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<string> 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)
      };
    }
  }
}

And this is how so set it up and how it will look once it's running:

Figure 1: Create a new Gutter item in the Core database:

Figure 2: Switch back to your Master database and activate the Gutter by right-clicking in the gutter area.

Figure 3: The Gutter now indicates the publication status of your items

From the top of my head it's not available out of the box. In the core database however, there's the definitions of the gutter, etc. You could create your own.

There's the 'published' field on items though, but I'm not sure if that takes different versions into account. Maybe you can check the differences between the item in the master and web (i.e. Item doesn't exist or is different version in web, then it's waiting to be published).

Alternatively, have a read through this: http://webcmd.wordpress.com/2011/08/31/sitecore-ribbon-that-displays-published-state-of-an-item/ It'll explain how to check if an item is published as a ribbon.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!