Search code examples
buttonoutlookcontextmenuribbon

How to add a RibbonButton to a context menu in Outlook?


I am at a loss how to add a custom button to a context menu in Outlook 2013. In Outlook 2007 and 2010 I manage to do that successfully with a CommandBarButton, but that is not possible anymore in 2013. Unfortunately, I could not find a tutorial how to convert my code or mitigate the issue.

My question now is: How can I create, add and access a custom RibbonButton that should appear in the Outlook context menu of mail, calendar and contact items?

I have already tried simple code like this:

<?xml version="1.0" encoding="utf-8" ?>
<customUI onLoad="Ribbon_Load" xmlns="http://schemas.microsoft.com/office/2009/07/customui">
  <contextMenus>
    <contextMenu idMso="ContextMenuMailItem">
      <button id="MyContextMenuContactItem"
           label="ContextMenuContactItem"
           onAction="OnMyButtonClick"/>
    </contextMenu>
  </contextMenus>
</customUI>

... but nothing happens, it is not displayed. The constructor of its .cs file is called, but nothing else.

If I try adding it programmatically via

Globals.Factory.GetRibbonFactory().CreateRibbonButton()

.. then I do not know how to add it to a preexisting context menu.

I must be missing something that is necessary to add the item to the context menu. Can anyone please point me to it?

PS: I am testing with Outlook 2007.


Solution

  • The problem was caused by a wrong stub that was automatically inserted by Visual Studio. The return value of GetCustomUI was always null because GetResourceText could not find the resource (although to me it looked valid). When I changed it to File.ReadAllText(Properties.Resources.MyRibbonXmlFile), Ribbon_Load was finally called.