11

I'm building a custom content editor button which will issue a command passing the ID of the selected item. Is there a way to enable/disable my button based on the template of the selected item. I want my command to only be run-able for items of a specific template. Obviously I could simply check the template ID of the item within my command handler code (which I will do anyway) but I would like to make this a bit more user-friendly.

As a (sort of similar) example, if you select an item with no children, the delete-subitems button under the main delete button disappears. I couldn't see any obvious mechanism about how this works though.

21

In your command there is a QueryState method where you can hide or disable buttons.

Return:

  • CommandState.Hidden to hide the button
  • CommandState.Disabled to disable the button
  • CommandState.Enabled to enable the button

An example is shown below:

public class MyCommand: Command
{
   public override void Execute(CommandContext context)
   {
      //main stuff here
   }

   public override CommandState QueryState(CommandContext context)
   {
      Error.AssertObject((object) context, "context");
      if (context.Items.Length == 0)
      {
         return CommandState.Disabled;
      }
      if (context.Items[0].TemplateID.ToString() != "{4F3D0700-F7E0-47E9-903E-FCA1CB08830C}")
      {
         return CommandState.Disabled;
      }
      return base.QueryState(context);
   }

}
9

Here is an alternative answer if you want to or are using Sitecore PowerShell Extensions for your context menu.

You can create a Context Menu script item in your SPE module and then using the Show if rules are met or not defined section of the Script item you can hide/show the menu based of a rule:

enter image description here

Then you can write your PowerShell script to do whatever you need to when the context menu is clicked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.