'Is Folder.PropertyAccessor safe to call from managed code?
We recently finished development of a VSTO Outlook add-in. For some configuration data, it uses custom olText properties on a Folder.
When our add-in detects that these properties are not available, it uses the UserDefinedProperties property of the Folder to Find/Add our custom properties if they don't exist.
if (folder.UserDefinedProperties.Find(propertyName) == null)
folder.UserDefinedProperties.Add(propertyName, OlUserPropertyType.olText);
While our add-in is running, we get and set these properties many times. We use the PropertyAccessor GetProperty and SetProperty methods to do that. Again, all of our properties are type OlUserPropertyType.olText. In addition, we always dispatch to Outlook's UI thread when we get and set the properties.
//set
string value = "blah";
folder.PropertyAccessor.SetProperty(GetSchemaName(propertyName), value);
// get
string value = folder.PropertyAccessor.GetProperty(GetSchemaName(propertyName)
GetSchemaName returns ("http://schemas.microsoft.com/mapi/string/" + assemblyGuid + "/" + propertyName).
However, our usage of GetProperty and SetProperty on the PropertyAccessor of the Folder results in frequent runtime COMExceptions. Here is an example exception for a call to GetProperty. We have seen this issue in both Outlook 2010 and 2013.
System.Runtime.InteropServices.COMException (0x80020005): Type mismatch.
(Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))
at Microsoft.Office.Interop.Outlook._PropertyAccessor.GetProperty(String SchemaName)
My question is this: Is there a known issue with using the Folder PropertyAccessor to get and set properties in VSTO? If not, why is this happening?
Solution 1:[1]
No, this is not a known issue.
GetSchemaName(propertyName)
What is the actual value passed to the Get/SetProperty methods?
Anyway, I'd suggest using the StorageItem class instead. Here is what MSDN states:
The GetStorage method obtains a StorageItem on a Folder object using the identifier specified by StorageIdentifier and has the identifier type specified by StorageIdentifierType. The StorageItem is a hidden item in the Folder, which roams with the account and is available online and offline.
If you specify the EntryID for the StorageItem by using the olIdentifyByEntryID value for StorageIdentifierType, then the GetStorage method will return the StorageItem with the specified EntryID. If no StorageItem can be found using that EntryID or if the StorageItem does not exist, then the GetStorage method will raise an error.
If you specify the message class for the StorageItem by using the olIdentifyByMessageClass value for StorageIdentifierType, then the GetStorage method will return the StorageItem with the specified message class. If there are multiple items with the same message class, then the GetStorage method returns the item with the most recent PR_LAST_MODIFICATION_TIME. If no StorageItem exists with the specified message class, then the GetStorage method creates a new StorageItem with the message class specified by StorageIdentifier.
If you specify the Subject of the StorageItem, then the GetStorage method will return the StorageItem with the Subject specified in the GetStorage call. If there are multiple items with the same Subject, then the GetStorage method will return the item with the most recent PR_LAST_MODIFICATION_TIME. If no StorageItem exists with the specified Subject, then the GetStorage method will create a new StorageItem with the Subject specified by StorageIdentifier.
Use the GetStorage method of the Folder class to get a StorageItem object on the parent Folder to store data for an Outlook solution.
See Storing Data for Solutions for more information.
Solution 2:[2]
You end up setting named MAPI properties on a folder. Keep in mind that while PST provider supports that, Exchange provider does not. You need to either reconsider your need to store the properties on the folder itself or set such properties on a hidden (associated) message in the folder. That is what MAPIFolder.GetStorage uses and that is how Outlook stores its own per-folder settings (such as views). Take a look at the existing data (Inbox is a good candidate) in OutlookSpy (I am its author) - click IMAPIFolder, go to the "Associated Contents" tab.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Wesley Wiser |
Solution 2 |