官术网_书友最值得收藏!

Adding a custom command to item context menu

The context menu is designed to increase the efficiency of content authors. The requirements may vary for content authors across different organizations.

In this recipe, we will create a menu item in the context menu of a Sitecore item that will display the number of children that the selected item contains. In the same way, you will be able to create a command button for the Content Editor and Experience Editor ribbons as well.

How to do it…

We will first create a Command class:

  1. In the SitecoreCookbook project, create a GetChildCount class in the Commands folder. Inherit the class from Sitecore.Shell.Framework.Commands.Command.
  2. Override the Execute() method of the Command class to apply a command action as follows:
    public override void Execute(CommandContext context)
    {
      if (context.Items.Length == 1)
      {
        Item currentItem = context.Items[0];
        SheerResponse.Alert(string.Format("Children count: {0}", currentItem.Children.Count));
      }
    }
  3. Override the QueryState() method of the Command class to show or hide the button, which means changing the state of the command based on its children count. Here, when an item does not have any children, we hide the command:
    public override CommandState QueryState(CommandContext context)
    {
      if (context.Items.Length == 1)
      {
        Item currentItem = context.Items[0];
        if (currentItem.Children.Count == 0)
          return CommandState.Hidden;
      }
      return base.QueryState(context);
    }
  4. Open the \App_Config\Commands.config file, and add the following command above the </configuration> node with the fully qualified name of the class created in step 1:
    <command name="item:getchildcount" type="SitecoreCookbook.Commands.GetChildCount, SitecoreCookbook" />
  5. Open the Sitecore desktop mode and switch the database to core. Open the Content Editor and select the /sitecore/Content/Applications/Content Editor/Context Menus/Default item. Create a new item, Child Count, in it using the /sitecore/templates/System/Menus/Menu item template.
  6. Enter field values in the item, as shown in the following image. Here, item:getchildcount is the command that we created in the Commands.config file in step 4:
    How to do it…
  7. Now, right-click on any item in the content tree of the Content Editor; you will get a new menu item Count Children in the context menu, as shown on the left-hand side of the following image. Clicking on it will show an alert box displaying the number of children that it contains:
    How to do it…

How it works…

The following image shows you how Sitecore renders the menu item in HTML. When the menu item is clicked, the scForm.postEvent() JavaScript method is called with parameters of the current event and an item:getchildcount(id=$Target) message, which passes these parameters to the Sitecore command engine:

How it works…

When Sitecore receives the message from the browser, it first interprets it and extracts the command name (item:getchildcount) and parameters (id={$Target}). Then, based on the commands from Commands.config, it invokes the Execute() method of the respective class where we can write our custom logic.

Here, if you see step 2, the Execute() method of Command contains a parameter of the CommandContext type, where we can get parameters passed from. We can also get multiple parameters passed from the command using context.Parameters["Parameter Name"];.

In step 3, we overrode the QueryState() method, which returns CommandState to the ribbon or context menu. CommandState is the state of the menu item, which we can set as CommandState.Hidden, CommandState.Enabled, or CommandState.Disabled to hide, enable, or disable the menu item accordingly.

There's more…

We can add a custom tab (ribbon), group (Strip), or button (Chunk) to the Content Editor ribbon under the /sitecore/Content/Applications/Content Editor/Ribbons item of the core database. The button on the ribbon will look like the following image:

There's more…

To create a custom button (Chunk) on the Content Editor ribbon, create an item named Count Children in /sitecore/Content/Applications/Content Editor/Ribbons/Chunks/Operations with the /System/Ribbon/Small Button item template and fill in fields as follows:

There's more…

In the same way, you can find all the buttons of the Experience Editor ribbon under the /sitecore/Content/Applications/WebEdit/Ribbons/WebEdit item. You can add different types of buttons to the ribbon. You can find more details about this at https://goo.gl/4nNSQu.

See also

Sitecore provides two frameworks—Sheer UI and SPEAK—to manage user interfaces. Here, we used the Sheer UI to show an alert message. We could also use SPEAK to show an alert or open a modal dialog, which you can learn from http://goo.gl/8rFKr5.

主站蜘蛛池模板: 荆州市| 镇宁| 定安县| 兴文县| 京山县| 金湖县| 巢湖市| 安达市| 磐安县| 柳林县| 漾濞| 武定县| 读书| 高碑店市| 宜章县| 蓝山县| 武汉市| 余江县| 家居| 邛崃市| 通渭县| 措美县| 轮台县| 缙云县| 邢台县| 丰镇市| 青神县| 桐梓县| 通州市| 神池县| 合作市| 玉林市| 故城县| 荥阳市| 德安县| 伽师县| 安图县| 台东县| 房产| 晋州市| 和硕县|