How to create the custom command in sitecore

By Shekhar Gigras

Three steps required to create the custome command in sitecore

  1. Create button in sitecore through content editor
  2. Create class with action for button click event
  3. Change in config file

Create button in sitecore through content editor

We create a button in our ribben for delete the 6 month backup folder from content tree

Steps for create the button in content tree

  1. Create a chunk
  2. Create a strip
  3. Create a ribbon

Create a chunk

  1. Login in sitecore, Go to desktop panel and select the core database (Why we are using desktop panel and core database, Reason is If we are changing anything related to the sitecore panel then we need to change in core database and change database option available in desktop panel only )
  2. click on chunk (/sitecore/content/Applications/Content Editor/Ribbons/Chunks)

1.PNG

  1. select the chunk node and right click and insert->Insert from template
  2. Choose /System/Ribbon/Chunk template, enter the Item Name "My Utility" or any
  3. select the "My Utility" node and right click and insert->Insert from template
  4. Choose /System/Ribbon/Large Button template, enter the Item name "Delete Old News"

1.PNG

If you want more command then follow the same steps

Create a strip

For this chunk, we need a strip where we display this command button like serialize, indexing tool

  1. click on strip (/sitecore/content/Applications/Content Editor/Ribbons/Strips)

1.PNG

  1. select the strip node and right click and insert->Insert from template
  2. Choose /System/Ribbon/Strip template, enter the Item Name "Custom Command" or any
  3. select the "Custom Command" node and right click and insert->Insert from template
  4. Choose /System/Reference template, enter the Item name "Custom Command"
  5. set reference value as per the figure

1.PNG

If you want more strips then follow the same steps

Create a ribbon

for this strip, we need a ribbon where we add this strip like Home, Publishing, Configure

  1. click on Ribbon and select Default node (/sitecore/content/Applications/Content Editor/Ribbons/Ribbons/Default)
  2. select the Default node and right click and insert->Insert from template
  3. Choose /System/Reference template, enter the Item name "Utility"
  4. set reference value as per the figure

1.PNG


Create class with action for button click event

  1. Create Class Library project in visual studio.
  2. Add sitecore Nuget Package from sitecore.org (Package: sitecore.kernel, sitecore.MVC)
  3. Add new item as a class
using Sitecore.Shell.Framework.Commands;
using Sitecore.Web.UI.Sheer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SitecoreUtilityHelper
{
    public class DeleteNewsCommand : Command
    {
        public override void Execute(CommandContext context)
        {
            Sitecore.Context.ClientPage.Start(this, "DeleteNews");
        }

        protected void DeleteNews(ClientPipelineArgs args)
        {
            if (!args.IsPostBack)
            {
                var url = string.Format("/Command/DeleteNews");
                SheerResponse.ShowModalDialog(url.ToString(), "900px", "600px", "", true);
                args.WaitForPostBack(false);
            }
        }
    }
}
  1. compile the code and copy the dll file in sitecore site bin folder (where you create the ribbon,strips and chunk)

Change in config file

  1. Go to "App_Config\Sitecore\CMS.Core" folder
  2. open "Sitecore.Commands.config" file
  3. add line according to the figure

1.PNG

Click Event you can find in chunk section

All steps is done.

1.PNG

Connect by Whatsapp

Posted in Sitecore on May 23, 2020


Comments

Please sign in to comment!