You have to use Callback methods (specified in the XML file) to control whether or not an item is visible. In fact there are lots of Callbacks available you can specify to custom your context menu in the XML file (and they all start with get). e.g. getEnabled, getImage, getDescription, getVisible, etc.
So for example to control whether or not a button in the Contact Item context menu is visible, you would set the getVisible XML attribute to call a method in the class object you returned in the CreateRibbonExtensibilityObject()
method (in the ThisAddIn.cs file).
Note
You need to make sure the object you return in the CreateRibbonExtensibilityObject()
method implements the MSOffice.IRibbonExtensibility
interface.
e.g.
public partial class ThisAddIn : IThisAddInView
{
protected override MSOffice.IRibbonExtensibility CreateRibbonExtensibilityObject()
{
return new MyRibbonCallbacks();
}
}
In the example below I tell the context menu button to call the GetVisible method to workout if it's visible or not.
<contextMenu idMso="ContextMenuContactItem">
<menuSeparator id="Separator1" />
<button id="AddNoteButton"
label="Add note"
onAction="OnActionButton"
getVisible="GetVisible"/>
</contextMenu>
And in the GetVisible method workout if the menu item should be visible or not
public class MyRibbonCallbacks : MSOffice.IRibbonExtensibility
{
public bool GetVisible(Microsoft.Office.Core.IRibbonControl control)
{
switch (control.Id)
{
case "AddNoteButton":
// Work out if the button should be visible or not.
// And return true for visible. And false for invisible
// Visible if current time is 11 o clock or after (just an example)
return DateTime.Now.Hour >= 11;
}
}
}