Search code examples
vstocontextmenuoutlook-addinribbon

VSTO Ribbon Context Menu Dynamic Hierarchy


How does one create a dynamic menu hierarchy using VSTO Ribbons (for Outlook 2016)?

The xml placeholder could look like this, but I need to add/remove a menu tree under the menu root (instead of dummySingle). It seems like there would need to be something like a "getDependents" callback for menu items.

<?xml version="1.0" encoding="UTF-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
  <contextMenus>
    <contextMenu idMso="ContextMenuMailItem">
       <menu id="Menu Root" label="Menu Root" >
        <button id="dummySingle"
            label="dummy"            
            onAction="DummyAction"/>            
      </menu >      
  </contextMenus>  
</customUI>

Solution

  • You would need to look at Dynamic Menus for Office Ribbon. This would be great source for you to start with: Adding Custom Dynamic Menus to the Office Fluent User Interface. Your ribbon XML will looks like ...

    <dynamicMenu id="dynamicMenu1" 
                     label="Dynamic Menu" 
                     getContent="GetContent" />
    

    And on GetContent handler you will build dynamic menu contexts, may looks like ...

    public string GetContent(IRibbonControl control)
    {
        StringBuilder MyStringBuilder = new StringBuilder(@"<menu xmlns=""http://schemas.microsoft.com/office/2006/01/customui"" >");
        MyStringBuilder.Append(@"<button id=""button1"" label=""Insert Text"" onAction=""OnAction""  imageMso=""SignatureLineInsert"" />");
        MyStringBuilder.Append(@"<menuSeparator id=""menusep1"" getTitle=""GetTitle"" />");
        MyStringBuilder.Append(@"<button id=""button2"" label=""Insert More Text"" onAction=""OnAction"" imageMso=""FileDocumentInspect"" />");
        MyStringBuilder.Append(@"</menu>");
        return MyStringBuilder.ToString();
    }
    

    More on Dynamic Menus: documentation for dynamicMenu