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
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);
}
}
}
-
It looks interesting. Can you tell where the configuration is added to this item? 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
-
-
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. Commented Oct 4, 2021 at 6:14
-
1I'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