3
votes

I have a little Outlook 2010 AddIn and need to restrict some actions on a custom folder:

  • Can I somehow prevent the user from renaming a folder?
  • Is it possible to prevent the user from moving any item to the folder? I know the ItemAdd Event, but this is only fired after the item was already moved. This is too late for me.
  • Would it be possible to disable the context menu (right click) of a folder?

Thanks a lot!

// edit: I think I found a solution for the context menu problem. Is that ok or is there a flaw in doing this?

outlook.FolderContextMenuDisplay += DisableArchiveFolderContextMenu;
private void DisableArchiveFolderContextMenu(Office.CommandBar commandBar, MAPIFolder folder)
{
  if (folder.Name.Equals(Settings.Default.ArchiveFolderName))
  {
    // Disable the context menu
    commandBar.Enabled = false;
  }
  else
  {
    commandBar.Enabled = true;
  } 
}
1

1 Answers

4
votes

Take a look at BeforeItemMove for preventing users from moving items into your folder. This event would need to be attached to every folder you are interested in watching - there is no global BeforeItemMove that I am aware of. You will have to recursively iterate each mailbox folder or latch onto Explorer.FolderSwitch and attach your item listener. The FolderSwitch has limitations though since you can still move items without switching folders via Inspector Ribbon, OWA, or EWS.

To keep users from renaming your folder, you can use the StorageItem to persist the Folder name implemented via FolderChange of the folders' context parent.

public partial class ThisAddIn
{
    Outlook.Folder folder;
    Outlook.Folder customFolder;
    Outlook.Folders mailbox;
    Outlook.Explorer explorer;

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        explorer = Globals.ThisAddIn.Application.ActiveExplorer();
        explorer.FolderSwitch += new Outlook.ExplorerEvents_10_FolderSwitchEventHandler(explorer_FolderSwitch);

        mailbox = Globals.ThisAddIn.Application.Session.DefaultStore.GetRootFolder().Folders;
        if (mailbox.Cast<Outlook.Folder>().Where(c => c.Name == "Custom Folder").Count() == 0)
        {
            customFolder = mailbox.Add("Custom Folder") as Outlook.Folder;
            Outlook.StorageItem si = customFolder.GetStorage("Custom Folder Storage", Outlook.OlStorageIdentifierType.olIdentifyBySubject);
            si.UserProperties.Add("PermanentFolderName", Outlook.OlUserPropertyType.olText).Value = customFolder.Name; // store persistent name
            si.Save();
        }
        else
            customFolder = mailbox["Custom Folder"] as Outlook.Folder;

        mailbox.FolderChange += new Outlook.FoldersEvents_FolderChangeEventHandler(mailbox_FolderChange);
    }

    void explorer_FolderSwitch()
    {
        folder = explorer.CurrentFolder as Outlook.Folder; // grab new handle
        folder.BeforeItemMove += new Outlook.MAPIFolderEvents_12_BeforeItemMoveEventHandler(folder_BeforeItemMove);
    }

    void mailbox_FolderChange(Outlook.MAPIFolder Folder)
    {
        Outlook.Folder folder = Folder as Outlook.Folder;
        Outlook.StorageItem si = folder.GetStorage("Custom Folder Storage", Outlook.OlStorageIdentifierType.olIdentifyBySubject);
        if (si.Size > 0 && si.UserProperties.Count > 0 && si.UserProperties["PermanentFolderName"].Value != folder.Name)
            folder.Name = si.UserProperties["PermanentFolderName"].Value; // override users name change
    }


    void folder_BeforeItemMove(object Item, Outlook.MAPIFolder MoveTo, ref bool Cancel)
    {
        if (MoveTo.Name == "Custom Folder")
            Cancel = true; // disallow moving items here
    }
}