1

I need to add a new button on the ribbon in ContentEditor for a specific template. It follows the instructions: https://community.sitecore.net/technical_blogs/b/sitecore_what39s_new/posts/adding-a-custom-button-to-the-ribbon The problem is the lack of information how I can associate such an operation with the template. This means: the new part of the menu will be shown, e.g. for the media folder, and for other templates it will not appear.

1 Answer 1

3

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);
        }
    }
}
5
  • It looks interesting. Can you tell where the configuration is added to this item?
    – Jan Nowak
    Commented Oct 1, 2021 at 12:12
  • @JanNowak no configuration, you need to override QueryState the same way you did the Execute method in the same class of your command.
    – AJ -
    Commented Oct 1, 2021 at 13:20
  • @JanNowak answer updated
    – Marek Musielak
    Commented Oct 3, 2021 at 8:31
  • Thanks a lot - it works great. I have one more question, is there any documentation that contains information about the methods and the possibility of writing them? Unfortunately, I do not see anything like that in the official documentation.
    – Jan Nowak
    Commented Oct 4, 2021 at 6:14
  • 1
    I'm not sure if there is any official documentation. But there are plenty of blog posts which are helpful in cases like this one.
    – Marek Musielak
    Commented Oct 4, 2021 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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