1

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.

SliverNinja - MSFT
  • 31,051
  • 11
  • 110
  • 173
dotNET
  • 33,414
  • 24
  • 162
  • 251

3 Answers3

1

My bad. I didn't read the exception message fully. It explicitly mentioned the illegal characters. These are:

Brackets: [ and ]
Underscore: _
Pound: #

Still if anyone has a better idea about storing folder level properties, please post them here.

SliverNinja - MSFT
  • 31,051
  • 11
  • 110
  • 173
dotNET
  • 33,414
  • 24
  • 162
  • 251
0

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.

SliverNinja - MSFT
  • 31,051
  • 11
  • 110
  • 173
  • that would be a great solution. can u plz confirm if it works only with Exchange mailboxes or any Outlook folder for that matter? – dotNET Oct 30 '12 at 14:04
  • I don't have any non-Exchange mailboxes to test it with - please post back and let us know. There are also [other Outlook storage solutions you may be interested in](http://stackoverflow.com/a/10175091/175679) if this doesn't work out for you. – SliverNinja - MSFT Oct 30 '12 at 14:07
0

According to the exception, the name cannot contain special characters. But the value of the property can:

Outlook.Folder folder = Application.GetNamespace("MAPI").Folders[1] as Outlook.Folder;
                Outlook.StorageItem storageItem = folder.GetStorage("ABCDE", Outlook.OlStorageIdentifierType.olIdentifyBySubject);
Outlook.UserProperty property = null;
foreach (Outlook.UserProperty p in storageItem.UserProperties)
{
    if (p.Name == "PropertyName")
        property = p;
}
if (property == null)
{
    property = storageItem.UserProperties.Add("PropertyName", Outlook.OlUserPropertyType.olText, false,                                                                Outlook.OlDisplayType.olUser);
                }
property.Value = "my_value_can_contain[brackets]";
storageItem.Save();
Bikash Bk
  • 1
  • 1