none
Possible to set UserProperty on Folder on Exchange Online? Getting exception RRS feed

  • Question

  • Hi,

    I'm developing Outlook Add-in which is well tested against local stores (.PST files). I have now set up a trial account for small businesses at Office 365 and try the same functionality against it.T he problem I'm encountering now is this:

    When attempting to set the value of a UserProperty on a Folder, the following exception occurs:

    Test method Tests.IntegrationManual.OutlookApiManualTest.CreateUpdateFolder_Test threw exception: 
    System.UnauthorizedAccessException:
    The property "http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/MyProperty" does not support this operation.

    The C# code line that triggers this error is as follows

    folder.PropertyAccessor.SetProperty(schemaName, value);

    (You can see the schemaName from the error message).
    I have successfully created the UserProperty using this line of code before attempting to set the actual property value:

    folder.UserDefinedProperties.Add("MyProperty", OlUserPropertyType.olText, Type.Missing, Type.Missing);

    Again, this works with folders on PST files but not on Exhange Online folders.

    So is a permission issue that I can resolve by configuring or altering my code? Or is this a shortcoming of Exchange Online?



    • Edited by FrodeNilsen2 Thursday, November 22, 2012 3:10 PM
    Thursday, November 22, 2012 3:09 PM

Answers

  • Exchange store does not support named properties on folders. Only messages can have named properties.

    If you want to store per-folder data, the usual solution is to create a hidden message with a custom message class in that folder and store any custom properies on that message.

    If you are using OOM, use MAPIFolder.GetStorage.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    • Marked as answer by FrodeNilsen2 Thursday, December 6, 2012 4:53 PM
    Wednesday, December 5, 2012 3:03 PM

All replies

  • Hi FordeNilsen2,

    Thanks for posting in the MSDN Forum.

    Would you please show more detailed code for further research? I never learned that Outlook works without local stores. I need your code to do further research.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Friday, November 23, 2012 5:38 AM
    Moderator
  • Ok, I may be wrong on the fact that Outlook is not using local stores - I merely state that an exception happens when the folder is created on an Exchange Online server. I don't know if Outlook caches or duplicates the folder locally.

    Here is a C# unit test code block that will reproduce the problem. Make sure you reference Microsoft.Interop.Outlook in the project.

            [TestMethod]
            public void ReproduceUnauthorizedAccessExceptionWhenAttemptingToSetUserPropertyOnExchangeOnlineFolder()
            {
                var app = new Application(); 
                // To reproduce, make sure an Exchange Online store is the default store, or use the GetStoreFromId()-code-line instead
                var store = app.GetNamespace("MAPI").DefaultStore;
                //var store = app.GetNamespace("MAPI").GetStoreFromID("yourStoreId");
    
                string PropertySchemaPrefix = "http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/";
                string folderName = "SomeNewFolder";
                string propertyKey = "MyUserProperty";
                string propertyValue = "SomeValue";
                string schemaName = PropertySchemaPrefix + propertyKey;
               
                var rootFolder = store.GetRootFolder();
                var folder = rootFolder.Folders.Add(folderName, OlDefaultFolders.olFolderTasks);
                var property = folder.UserDefinedProperties.Find(propertyKey);
                if (property == null) folder.UserDefinedProperties.Add(propertyKey, OlUserPropertyType.olText, Type.Missing, Type.Missing);
                // The following line causes a System.UnauthorizedAccessException when executed on an Exhange Online store, but not on a local store
                folder.PropertyAccessor.SetProperty(schemaName, propertyValue);
            }


    Wednesday, November 28, 2012 10:11 AM
  • Where are you running this code? Is it in an Outlook addin? If so do not ever create a new Outlook.Application object, use the trusted Application object passed to you in your addin's startup code.
     
    If you use OutlookSpy or MFCMAPI do you see the user property created on the Folder object?
     
    Can you access the property in Outlook VBA code using the UserDefinedProperties collection of the Folder?
     
    Does the following VBA sample code from the Outlook Object Browser Help work for you?
     
    Sub AddStatusProperties()
        Dim objNamespace As NameSpace
        Dim objFolder As Folder
        Dim objProperty As UserDefinedProperty
        
        ' Obtain a Folder object reference to the
        ' Inbox default folder.
        Set objNamespace = Application.GetNamespace("MAPI")
        Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)
    
        ' Add five user-defined properties, used to identify and
        ' track customer issues.
        With objFolder.UserDefinedProperties
            Set objProperty = .Add("Issue?", olYesNo, olFormatYesNoIcon)
            Set objProperty = .Add("Issue Research Time", olDuration)
            Set objProperty = .Add("Issue Resolution Time", olDuration)
            Set objProperty = .Add("Customer Follow-Up", olYesNo, olFormatYesNoYesNo)
            Set objProperty = .Add("Issue Closed", olYesNo, olFormatYesNoYesNo)
        End With
    End Sub

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "FrodeNilsen2" <=?utf-8?B?RnJvZGVOaWxzZW4y?=> wrote in message news:5506e420-10d5-4f35-a5b7-765450701607...

    Ok, I may be wrong on the fact that Outlook is not using local stores - I merely state that an exception happens when the folder is created on an Exchange Online server. I don't know if Outlook caches or duplicates the folder locally.

    Here is a C# unit test code block that will reproduce the problem. Make sure you reference Microsoft.Interop.Outlook in the project.

            [TestMethod]
            public void ReproduceUnauthorizedAccessExceptionWhenAttemptingToSetUserPropertyOnExchangeOnlineFolder()
            {
                var app = new Application(); 
                // To reproduce, make sure an Exchange Online store is the default store, or use the GetStoreFromId()-code-line instead
                var store = app.GetNamespace("MAPI").DefaultStore;
                //var store = app.GetNamespace("MAPI").GetStoreFromID("yourStoreId");
    
                string PropertySchemaPrefix = "http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/";
                string folderName = "SomeNewFolder";
                string propertyKey = "MyUserProperty";
                string propertyValue = "SomeValue";
                string schemaName = PropertySchemaPrefix + propertyKey;
               
                var rootFolder = store.GetRootFolder();
                var folder = rootFolder.Folders.Add(folderName, OlDefaultFolders.olFolderTasks);
                var property = folder.UserDefinedProperties.Find(propertyKey);
                if (property == null) folder.UserDefinedProperties.Add(propertyKey, OlUserPropertyType.olText, Type.Missing, Type.Missing);
                // The following line causes a System.UnauthorizedAccessException when executed on an Exhange Online store, but not on a local store
                folder.PropertyAccessor.SetProperty(schemaName, propertyValue);
            }



    Ken Slovak MVP - Outlook
    Thursday, November 29, 2012 7:20 PM
    Moderator
  • Where are you running this code? Is it in an Outlook addin? If so do not ever create a new Outlook.Application object, use the trusted Application object passed to you in your addin's startup code.

    This code is running in unit test only and was created for the purpose of this forum post. In production code I use the trusted Application object.

     
    If you use OutlookSpy or MFCMAPI do you see the user property created on the Folder object?

    Yes I can.

     
    Can you access the property in Outlook VBA code using the UserDefinedProperties collection of the Folder?

    Yes.

     
    Does the following VBA sample code from the Outlook Object Browser Help work for you?

    No, because that code is only creating the property - not setting the value. If I'm going to try some VB code here - your example would need this additional line before the "End Sub" in order to reproduce the problem I'm talking about:

    objFolder.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/Issue Closed", true)

    (Assumption made: boolean "true" is a valid input for OlUserPropertyType.olYesNo. I haven't actually tested this code)


    • Edited by FrodeNilsen2 Wednesday, December 5, 2012 10:54 AM
    Wednesday, December 5, 2012 10:49 AM
  • Exchange store does not support named properties on folders. Only messages can have named properties.

    If you want to store per-folder data, the usual solution is to create a hidden message with a custom message class in that folder and store any custom properies on that message.

    If you are using OOM, use MAPIFolder.GetStorage.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    • Marked as answer by FrodeNilsen2 Thursday, December 6, 2012 4:53 PM
    Wednesday, December 5, 2012 3:03 PM
  • That worked, Dmitry- thanks! So to sum up, this is how the final part of my code ended up being: 

    /// (...) see code above on how I set folder and key StorageItem storage = folder.GetStorage(key, OlStorageIdentifierType.olIdentifyBySubject); UserProperty prop = storage.UserProperties[propertyKey];
    if (prop == null) prop = storage.UserProperties.Add(key, OlUserPropertyType.olText, Type.Missing, Type.Missing);
    prop.Value = propertyValue;

    storage.Save(); Marshal.ReleaseComObject(storage);


    -The storage.Save()-part had me for an hour...


    Thursday, December 6, 2012 4:57 PM