How to: Ensure that Custom Item Properties Are Supported in Folder-Level Queries

This example shows how to ensure that when you add a custom property to an item type, you also add the property to the folder so that you can query on that custom property at the folder level.

This code sample shows how to use the UserDefinedProperties object and the UserDefinedProperty object to ensure that when you add a custom property to an item type, you also add the property to the folder so that you can query on that custom property at the folder level.

When you use the Add method on the UserProperties collection to add a custom property to an item, you can specify the AddToFolderFields parameter as True to add the property to the folder. However, the custom property might not get added to the desired folderbecause of developer error or a user action, such as removing the custom property through the Outlook Field Chooser or moving the item to another folder. Consequently, the Find method and the Restrict method of the Items object that uses that custom property will fail. By using the UserDefinedProperties object, you can test whether your custom properties exist in the folder, and add the custom properties if they do not exist or if they have been removed.

To persist a custom property represented by a UserDefinedProperty object in a folder, you must save the custom property with the same name in the item. Storing a value in the UserDefinedProperty object for the folder has no effect. You must se the item's UserProperties collection to access the UserProperty object that you want to set, and then set the value on the UserProperty object. Be sure to call the Save method on the item to persist your changes.

If you use Microsoft Visual Studio to test this code example, you must first add a reference to the Microsoft Outlook 14.0 Object Library component and specify the Outlook variable when you import the Microsoft.Office.Interop.Outlook namespace. The Imports or using statement must not occur directly before the functions in the code example but must be added before the public Class declaration. The following lines of code show how to do the import and assignment in Visual Basic and C#.

using Outlook = Microsoft.Office.Interop.Outlook;
private void DemoUserDefinedProperty()
{
    Outlook.Folder folder =
        Application.ActiveExplorer().CurrentFolder
        as Outlook.Folder;
    Outlook.PostItem post = folder.Items.Add("IPM.Post")
        as Outlook.PostItem;
    // Add UserProperty to PostItem
    post.UserProperties.Add("ColorID",
        Outlook.OlUserPropertyType.olText,
        false, Type.Missing);
    post.UserProperties["ColorID"].Value = "Green";
    post.Subject = "UserProperty Example";
    post.Save();
    Outlook.PostItem findPost;
    try
    {
        // Items.Find will fail unless custom property
        // is defined in the folder
        findPost =
            folder.Items.Find("[ColorID] = 'Green'")
            as Outlook.PostItem;
    }
    catch (Exception ex)
    {
        Debug.WriteLine(ex.Message);
    }
     // Add ColorID field to the folder
    folder.UserDefinedProperties.Add("ColorID",
        Outlook.OlUserPropertyType.olText,
        Type.Missing, Type.Missing);
    // Now the find works ok
    Outlook.PostItem findPostOK;
    try
    {
        findPostOK =
            folder.Items.Find("[ColorID] = 'Green'")
            as Outlook.PostItem;
        if (findPostOK != null)
        {
            Debug.WriteLine("Found PostItem");
        }
        // Cleanup by deleting PostItem and ColorID
        findPostOK.Delete();
        Outlook.UserDefinedProperty userProperty =
            folder.UserDefinedProperties["ColorID"];
        userProperty.Delete();
    }
    catch (Exception ex)
    {
        Debug.WriteLine(ex.Message);
    }
}

Other Resources

Community Additions

ADD
Show:
© 2014 Microsoft