Sitecore - Hide Button in Ribbon

2019-05-06 23:00发布

问题:

I created a Contextual Ribbon for a specific content item. I have two buttons that will "promote" or "demote" the item to a certain category (there can only be one in the section).

Is it possible to hide one of the buttons based on the contents state in some sort of code behind? I understand how to link up to the Click event, but I was wondering if there was some sort of load event for the custom ribbon to access.

回答1:

Seems I can use the same class that inherits from Command to override the QueryState method. This is called when the buttons are loaded, and I can do a check there and return a CommandState.Hidden if the button shouldn't be shown

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

    return item.Fields["Spotlight"].Value == "" ? CommandState.Hidden : base.QueryState(context);
}


标签: c# .net sitecore