Outlook vsto - 文件夹限制

时间:2013-06-21 10:58:46

标签: outlook vsto add-in

我有一点Outlook 2010 AddIn,需要限制自定义文件夹上的某些操作:

  • 我可以以某种方式阻止用户重命名文件夹吗?
  • 是否可以阻止用户将任何项目移动到该文件夹​​?我知道ItemAdd事件,但只有在项目已经移动后才会触发。这对我来说太晚了。
  • 是否可以禁用文件夹的上下文菜单(右键单击)?

非常感谢!

//编辑:我想我找到了上下文菜单问题的解决方案。这样做还是存在缺陷吗?

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 个答案:

答案 0 :(得分:4)

请查看BeforeItemMove以防止用户将项目移动到您的文件夹中。此事件需要附加到您有兴趣观看的每个文件夹中 - 我不知道全局BeforeItemMove。您必须递归迭代每个邮箱文件夹或锁定到Explorer.FolderSwitch并附加您的项侦听器。 FolderSwitch有限制,因为您仍然可以通过Inspector功能区,OWA或EWS移动项目而无需切换文件夹。

要防止用户重命名您的文件夹,您可以使用StorageItem保留通过文件夹'上下文父级FolderChange实现的文件夹名称。

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
    }
}