Skip to main content
added 837 characters in body
Source Link
Marek Musielak
  • 24.2k
  • 3
  • 31
  • 64

You can override QueryState method like that:

public override CommandState QueryState(CommandContext context)
{
  var item = context.Items[0];

  if (item.TemplateID != MyTemplateId)
    return CommandState.Hidden;

  return base.QueryState(context);
}

EDIT

After OP's question. There is no additional configuration needed. It's just one more method you need to override. So if you copied code from the link you added in your question and wrote your custom code in Execute method, you need to do the same with QueryState method:

using Sitecore.Shell.Framework.Commands;
using System;

namespace Sc.Int.MiniBizz.Customaztion
{
    class MBCustomButtonImport : Command
    {
        public override void Execute (CommandContext context)
        {
            Sitecore.Context.ClientPage.ClientResponse.Alert("Testing my button");
        }
        
        public override CommandState QueryState(CommandContext context)
        {
          var item = context.Items[0];

          if (item.TemplateID != MyTemplateId)
            return CommandState.Hidden;

          return base.QueryState(context);
        }
    }
}

You can override QueryState method like that:

public override CommandState QueryState(CommandContext context)
{
  var item = context.Items[0];

  if (item.TemplateID != MyTemplateId)
    return CommandState.Hidden;

  return base.QueryState(context);
}

You can override QueryState method like that:

public override CommandState QueryState(CommandContext context)
{
  var item = context.Items[0];

  if (item.TemplateID != MyTemplateId)
    return CommandState.Hidden;

  return base.QueryState(context);
}

EDIT

After OP's question. There is no additional configuration needed. It's just one more method you need to override. So if you copied code from the link you added in your question and wrote your custom code in Execute method, you need to do the same with QueryState method:

using Sitecore.Shell.Framework.Commands;
using System;

namespace Sc.Int.MiniBizz.Customaztion
{
    class MBCustomButtonImport : Command
    {
        public override void Execute (CommandContext context)
        {
            Sitecore.Context.ClientPage.ClientResponse.Alert("Testing my button");
        }
        
        public override CommandState QueryState(CommandContext context)
        {
          var item = context.Items[0];

          if (item.TemplateID != MyTemplateId)
            return CommandState.Hidden;

          return base.QueryState(context);
        }
    }
}
Source Link
Marek Musielak
  • 24.2k
  • 3
  • 31
  • 64

You can override QueryState method like that:

public override CommandState QueryState(CommandContext context)
{
  var item = context.Items[0];

  if (item.TemplateID != MyTemplateId)
    return CommandState.Hidden;

  return base.QueryState(context);
}