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