1
votes

Is it possible to include an If statement in a VSTO plugin that will offer up different Context Menu items based on the Current User's attributes, e.g. Department?

I am attempting to use the documentation in here https://docs.microsoft.com/en-us/office/client-developer/outlook/pia/how-to-get-information-about-the-current-user to add to my existing code:

 <contextMenu idMso="ContextMenuMailItem">
      <menu id="ReplyListContext" label="Choose a Quick Reply" insertBeforeMso="Copy">
        <button id="CharityContext" label="Charity Request" onAction="Charity_Click"/>
>
      </menu>
    </contextMenu>

 public void Charity_Click(Office.IRibbonControl control)
        {
            Outlook.Explorer explorer = Globals.ThisAddIn.Application.ActiveExplorer();
            if (explorer != null)
            {
                Outlook.Selection selection = explorer.Selection;
                if (selection.Count >= 1)
                {
                    Outlook.MailItem mailItem = selection[1] as Outlook.MailItem;
                    if (mailItem != null) //could be something other than MailItem

                    {
                        Outlook.MailItem response = mailItem.ReplyAll();

                        response.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;

                        response.HTMLBody = "<p>Thanks for your e-mail</p><p>text text text</p><p>Many thanks</p> " + response.HTMLBody;
                        response.Send();
                    }
                }
            }
        }

I have a number of these responses and want different departments to be presented with different options when they open Outlook

1

1 Answers

0
votes

You could use DynamicMenu

  1. In your ribbonXML, add a dynamicmenu item:

    <contextMenus>
            <contextMenu idMso='ContextMenuCell'>
                <dynamicMenu id='dynamicRoot' label='Sample Dynamic' getContent='GetMenuCustomContent' /> 
            </contextMenu>
    </contextMenus>
    
  2. Your callback function GetMenuCustomContent will be called whenever the hosting app (Outlook) needs to build the menu.

  3. Return different XML according to the CurrentUser.

    public string GetMenuCustomContent(IRibbonControl control)
    {
        // here get the current user and return different XML
        // accodring to any condition
    
        if(SomeOtherCondition)
        {
            return @"<menu xmlns='http://schemas.microsoft.com/office/2009/07/customui'>
                        <button id='some_button' label='UserType1' onAction='DoSomething'/>
                    </menu>";
        }
        else if (SomeOtherCondition)
        {
            return @"<menu xmlns='http://schemas.microsoft.com/office/2009/07/customui'>
                        <button id='some_button2' label='UserType2' onAction='DoSomething2'/>
                    </menu>";
        }
    
        return "";
    }