public static Options LoadFromConfigItem() { // grab the FAI message that houses user config Outlook.MAPIFolder inboxFolder = Globals.ThisAddIn.Application.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox); Outlook.StorageItem configItem = inboxFolder.GetStorage(Options.CONFIG_MESSAGE_SUBJECT, Outlook.OlStorageIdentifierType.olIdentifyBySubject); XmlSerializer x = new XmlSerializer(typeof(Options)); using (StringWriter writer = new StringWriter()) { // we found existing user config so deserialize the stored XML into an Options object for add-in to use if (configItem.EntryID != null && configItem.EntryID != "") { Log.WriteEntry("User options were loaded from Exchange."); // if we fail to deserialize, just return an empty one Options opts; try { opts = (Options)x.Deserialize(new StringReader(configItem.Body)); } catch { MessageBox.Show("There was an error reading your WFM for Outlook settings from Exchange so it has been reset. Please update your settings again accordingly."); opts = new Options(); } return(opts); } } return(null); }
/// <summary> /// Persist this object to Exchange as a StorageItem. /// </summary> public void Save() { Outlook.MAPIFolder inboxFolder = Globals.ThisAddIn.Application.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox); Outlook.StorageItem configItem = inboxFolder.GetStorage(Options.CONFIG_MESSAGE_SUBJECT, Outlook.OlStorageIdentifierType.olIdentifyBySubject); configItem.Subject = Options.CONFIG_MESSAGE_SUBJECT; XmlSerializer x = new XmlSerializer(typeof(Options)); using (StringWriter writer = new StringWriter()) { // serialize this object into XML and store into the config item's body property x.Serialize(writer, this); configItem.Body = writer.ToString(); // persist the item to Exchange configItem.Save(); Log.WriteEntry("User options were saved to Exchange."); } }