How to create and use Sitecore command

  1. Create a class which inherits below class Sitecore.Shell.Framework.Commands.Command

2. Below line of code is actually used to call a method which handles the action on a yes/no/cancel dialog box on the current sitecore page

Sitecore.Context.ClientPage.Start(this, nameof(DialogProcessor));

3. Below lines of code opens up a dialog box to get an input from the user to trigger/cancel the command execution and wait for user input.

SheerResponse.YesNoCancel(“Are you sure you want to sync products?”, “500px”, “200px”, true);


4. In the else part of the above code, there is the condition to check if user has input “yes”. If yes, then “SyncProductsInMaster” method will be called.

5. Below is the screenshot for the “SyncProductsInMaster” method which will be calling another class’s method where the logic to sync products is.

Below line of code logs the command execution start messaage in Sitecore logs.

Sitecore.Diagnostics.Log.Info(“Product Sync sitecore command start”, this);

We have used the dependency injection in our project. But in a sitecore command it is not possible to do constructor dependency injection. So below is another way to get the instances. We used Sitecore.DependencyInjection.ServiceLocator to get the instances for required dependencies. Below statement is actually used to get the instance of IServiceAccess

var service = ServiceLocator.ServiceProvider.GetService(typeof(IServiceAccess)) as ServiceAccess;

6. Create a patch file in App_Config/Include folder to add this newly created command in to system.

To add button in the ribbon to trigger the command follow the steps below:-

  1. Created a Products Chunk using template /sitecore/templates/System/Ribbon/Chunk at location at below location /sitecore/content/Applications/Content Editor/Ribbons/Chunks

3. Create a large button under Products Chunk and add the the command name which we was given in the patch file create above.

4. Create an “Admin” strip using template /sitecore/templates/System/Ribbon/Strip at location /sitecore/content/Applications/Content Editor/Ribbons/Strips

5. Create a reference item at path /sitecore/content/Applications/Content Editor/Ribbons/Ribbons/Default so that admin strip created in previous step can be added to default ribbon bar.

Finally you can trigger your command by clicking the Sync button in Admin Tab of ribbon bar which will open a dialog box to ask user for confirmation to sync products. See the screenshot below.