Search code examples
c#outlookvstooutlook-addinoutlook-2010

Allowed characters in UserDefinedProperty's Name property


I'm adding a custom property AT FOLDER LEVEL in Outlook 2010. MAPIFolder (and Folder) object has a property named UserDefinedProperties where one can add custom properties, but the problem is that these properties are not meant to store values with them. As a hack I was storing property value right within the name by separating the two with a EQUAL sign, e.g. I'd add a UserDefinedProperty whose Name would be something like "MyProperty=123".

Now the problem is that sometime the value of my property contains characters that are not allowed in the Name. For example, I have a property whose value is "America/New_York". These two characters (slash and underscore) are not allowed in the Name, so I get an exception.

What I need here is either a better way to store a property value at folder level, or alternately, a list of allowed characters in the Name property of a UserDefinedProperty object, so that I could do some kind of replacement.

I'm using C#, .NET Fx 4.0 and VSTO.


Solution

  • You should use StorageItem for managing Folder-level state. StorageItems are hidden from the users view and allow you to persist state using an Exchange mailbox item.

    Persist Folder State via StorageItem using a MessageClass key

    Outlook.StorageItem folderState = folder.GetStorage("IPM.Storage.MyCustomStore", Outlook.OlStorageIdentifierType.olIdentifyByMessageClass);
    if (folderState.Size == 0) // no state exists
    { // save state
      folderState.UserProperties.Add("CustomKey1", Outlook.OlUserPropertyType.olText).Value = "America/New_York";
      folderState.Save();
    }
    else // state exists
    { // read state
      string propVal = folderState.UserProperties["CustomKey1"].Value;
    }
    

    You can manage StorageItems for a folder using Subject as a key or using MessageClass as a key in the example above.