Sitecore Ribbon: How to Add a Publishable Button

August 26, 2014

Blog | Development | Sitecore Ribbon: How to Add a Publishable Button
Sitecore Ribbon: How to Add a Publishable Button

This tutorial will cover how to create a checkbox button for the Sitecore content editor ribbon, to control whether or not an item may be published. It is expected that you should already have a decent familiarity with using Sitecore (adding/editing items, the master/core databases, etc.).


The various pieces we will need to make/modify:

Sitecore

To begin, log in to the Sitecore shell, and switch to the Core database.

  1. Create a new Chunk item under /sitecore/content/Applications/Content Editor/Ribbons/Chunks, named “Item Publishing”. The Chunk items represent the labeled subsections in each ribbon tab, which contain the various tools and buttons. The path to the Chunk template is /sitecore/templates/System/Ribbon/Chunk.

chunk

Assign the following values for the item’s fields:

  1. Header: Item Publishing
  2. ID: ItemPublishingChunk

 itempublishing

  1. Create a new Small Check Button item under the newly created Chunk item, named “Publishable”. This is the item that represents the actual checkbox that will appear in the ribbon, inside the “Item Publishing” chunk. The path to the Small Check Button template is /sitecore/templates/System/Ribbon/Small Check Button.

 ItemsPublishable

 
Assign the following values for the item’s fields:

  1. Header:           Publishable
  2. Click:               custom:togglepublishable
  3. Tooltip:            Restrict the publication of the current item

publishable2 

  1. Create a new Reference item under /sitecore/content/Applications/Content Editor/Ribbons/Strips/Publish. Each Strip item represents one of the tabs in the ribbon, and the Reference items are used to specify the Chunks that appear on each tab. The path to the Reference template is /sitecore/templates/System/Reference.

 ItemsPublishing4

In the “Reference” field, select the newly created Chunk.

 

In the ribbon, under the “Publish” tab, there should now be a new section named “Item Publishing” that contains the “Publishable” checkbox. Now we just need to add the code that should execute upon the checkbox being clicked…

Visual Studio

The following steps are done inside the Visual Studio project associated with the site.

 

  1. Create a new Class named “TogglePublishableCommand” that inherits from Sitecore.Shell.Framework.Commands.Command. This will be responsible for handling the checkbox functionality.
    
    
    using Sitecore.Data.Items;
    using Sitecore.Diagnostics;
    using Sitecore.Shell.Framework.Commands;
    using System;
    
    namespace Custom.Commands
    {
        [Serializable]
        public class TogglePublishableCommand : Sitecore.Shell.Framework.Commands.Command
        {
            
    
    
        }
    }
    

    a) Override the Execute method. This method will be called whenever the checkbox is clicked. We will want to flip the state of whether the context item is publishable.

    public override void Execute(CommandContext context)
    {
        Assert.ArgumentNotNull(context, "context");
        if (context.Items.Length == 1)
        {
            Item item = context.Items[0];
            item.Editing.BeginEdit();
            item.Publishing.NeverPublish = !item.Publishing.NeverPublish;
            item.Editing.EndEdit();
    
            // reload the page
            string reload = string.Format("item:load(id={0})", item.ID);
            Sitecore.Context.ClientPage.ClientResponse.Timer(reload, 1);
        }
    }
    

    b) Override the QueryState method. This method is used to determine whether the checkbox displays as checked/unchecked.

    public override CommandState QueryState(CommandContext context)
    {
        Assert.ArgumentNotNull(context, "context");
        if (context.Items.Length == 1)
        {
            Item item = context.Items[0];
            if (!item.Publishing.NeverPublish)
            {
                // checkbox is checked
                return CommandState.Down;
            }
        }
        // checkbox is unchecked
        return CommandState.Enabled;
    }
    
  2. In Commands.Config, add a line to reference the new command class. This matches up the command name used in Sitecore with the associated code class and assembly.
    <command name="custom:togglepublishable" type="Custom.Commands.TogglePublishableCommand,Custom.dll"/>
  3. Save and build the project.

That’s it! The “Publishable” checkbox we created earlier should now function to restrict/allow publishing of the current item.

finished 

Dave Taylor

Developer
Tags
  • Sitecore
  • Tutorial

Recent Work

Check out what else we've been working on