Thursday, 30 January 2014

How to add Recycle Bin button to sitecore ribbon?

Friends, this is tidbit thing about sitecore. Sitecore newbie who wants to know how to add new command in ribbon (in sitecore- we call button as "command") will find this post useful.

Have you ever imagined from where all sitecore buttons are coming? Do you want to create your own sitecore command/button? If so, then after reading this post you will be able to achieve it.


Sometimes you want to add your own command in sitecore to introduce new feature or may be you want to replicate existing command to new location, in such case you can take advantage of sitecore flexible architecture to hook your own logic.

How to achieve it?
 Today we will take example of existing sitecore command (you know "command" means "button" in sitecore). Do you know where is "RecycleBin" command in sitecore content editor? Can't find in ribbon? Well, it is in sitecore menu. Here it is:

Whenever you want to see deleted items of sitecore, you need to click on "sitecore" button and then select "Recycle Bin" - two clicks.. huh..

What if this can be achieved by one click? What if you can set "Recycle Bin" button in ribbon? This may increase your efficiency.

How about this?

Like it? 
Then let's see how to configure it in your environment? It's very simple and that is what I like most about sitecore. One of the best things about sitecore is it is fully customizable. You can hook your logic anywhere in sitecore.
In upcoming posts, we are going to see couple of customization and I am sure you will also like it.

Here are steps:
1) Switch to "core" database
2) Go to "/sitecore/content/Applications/Content Editor/Ribbons/Chunks/Operations" path
3) Add a new item of type "/sitecore/templates/System/Ribbon/Large Button", give any name to this item (for example: "Recycle Bin")
4) Add "click" action as "item:recyclebin" and set other parameter as per your choice.

Refer below screen-shot for more details:

 5) Now let's add code for "item:recyclebin" command.
To add this code, we need to create a new command by inheriting from sitecore's command class.
 public class RecycleBinCommand: Command
        public override void Execute(CommandContext context)
            Assert.ArgumentNotNull(context, "context");
            Windows.RunApplication("Archives/Recycle Bin", "");
 If you want to look at how my visual studio solution explorer after adding this new command, then here you go:

6) The last step is to add above "RecycleBinCommand" in commands.config file. Here we will tell sitecore that "item:recyclebin" should call "RecycleBinCommand" class.

 That's it, you are about to go. Click on RecycleBin button and you should see dialog box like this:

Thanks to my colleague Azamat for reviewing my first draft of this post.

Customize sitecore, increase your efficiency !!!

How to publish referenced (linked) items in sitecore?

Have you come across a situation where you want to publish all referenced (linked) items while publishing any item in sitecore? If yes, then you are at correct place. Continue reading...

The default behavior of sitecore is to publish only selected item. Now think that if you have added few images to sitecore item and you want to publish these newly added (linked) images automatically when you publish that item.
How can you achieve it? Well, answer is - we need to pick all references of publishing item and add all these reference items to publishing queue pro-grammatically. That's it.

How to achieve it?
namespace Customization.Sitecore
    public class PublishReferenceItemsPipeline : PublishProcessor
        private Database _sourceDatabase;
        public override void Process(PublishContext context)
            Assert.ArgumentNotNull(context, "context");
            Log.Info(GetType().FullName + ": Adding items to publish queue - START", this);
            _sourceDatabase = context.PublishOptions.SourceDatabase;
            IEnumerable<ID> publishQueueItems = FetchPublishQueueItems(context.PublishOptions);
            IEnumerable<ID> publishQueueReferencedItems = publishQueueItems.SelectMany(GetReferences).ToArray();
            IEnumerable<PublishingCandidate> publishingCandidates = publishQueueReferencedItems.Select(itemId => new PublishingCandidate(itemId, context.PublishOptions)).ToArray();
            Log.Info(GetType().FullName + ": Adding items to publish queue - END", this);

        private IEnumerable<ID> FetchPublishQueueItems(PublishOptions options)
            if (options.Mode == PublishMode.Incremental)
                return PublishQueue.GetPublishQueue(options).Select(candidate => candidate.ItemId).ToArray();
            return PublishQueue.GetContentBranch(options).Select(candidate => candidate.ItemId).ToArray();

        private IEnumerable<ID> GetReferences(ID itemID)
            Item item = _sourceDatabase.GetItem(itemID);
            if (item == null)
                return Enumerable.Empty<ID>();
            ItemLink[] references = Globals.LinkDatabase.GetReferences(item);
            return references.Select(reference => reference.TargetItemID).ToArray();
Next step is to hook this newly created processor to Sitecore.Publishing.Pipelines.Publish.AddItemsToQueue processor of sitecore.
We have 2 options to hook our processor:

1) Add <processor type="Customization.Sitecore.PublishReferenceItemsPipeline, Customization.Sitecore" /> after <processor type="Sitecore.Publishing.Pipelines.Publish.AddItemsToQueue, Sitecore.Kernel"/> in web.config file.

2) If you don't want to touch web.config then second option is suitable for you. Add a .config file under App_Config folder.

Give any meaningful name to it. I will name it like "PublishingReference.config"
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:patch="">
        <processor type="Customization.Sitecore.PublishReferenceItemsPipeline, Customization.Sitecore" patch:after="*[@type='Sitecore.Publishing.Pipelines.Publish.AddItemsToQueue, Sitecore.Kernel']"/>

Customize your publishing, make your life easy !!!