Filtering a Custom Field

Outlook Developer Reference

You can specify custom properties in filters using Microsoft Jet syntax or DAV Searching and Locating (DASL) syntax. The custom properties must be defined in the folder where you are applying the filter. If the custom properties are only defined in the item, the search will fail.

Jet Queries

Custom properties can contain spaces in the property name. In a Jet query, as in all property name references, simply enclose the custom property name in square brackets. For example, the following Jet query retrieves all contacts where the custom property named "Preferred Gift" is exactly "Diamonds". For the query to succeed, the custom property named "Preferred Gift" has been defined in the folder that contains the custom contact items:

  criteria = "[Preferred Gift] = 'Diamonds'"

DASL Queries

In a DASL query, if the name of a custom property contains spaces, you must apply Uniform Resource Locator (URL) encoding to each space character and replace the space with "%20". In general, URL encoding applies the same way to characters in a DASL query as in a URL.

When you construct a DASL query for a custom property, you must use the namespace GUID for Outlook custom properties in the following format:{GUID}/PropertyName

where {GUID} is the following GUID:


Filtering Custom Properties Referenced by the MAPI String Namespace

If the custom property you are filtering for does not exist in the UserDefinedProperties collection for the folder, and if you are referencing the custom property by the MAPI string namespace, then you must explicitly append a type specifier to the namespace representation of the custom property. Note that you need to specify the type only when applying a DASL filter to search and filter entry points in the Items collection and the Table object, and to the Application.AdvancedSearch method.

Bb147591.vs_note(en-us,office.12).gif  Note
The hexagonal type specifier must be of the form 0000HHHH with only 8 digits as opposed to 9. For more information on the hexagonal type specifiers (HHHH) for various MAPI types, see "MAPI Property Type Specifiers" below.

For example, if you want to use Items.Restrict to search for the custom Unicode string property named "MyProperty" and this property does not exist in the UserDefinedProperties collection for the folder, you must append the Unicode string type specifier, 0000001f, to the representation of the property in the MAPI string namespace:

  criteria = "@SQL=" & Chr$(34) & "" _
& "{00020329-0000-0000-C000-000000000046}/MyProperty"_
& "/0000001f" & Chr(34) & " = '12-74440'"

MAPI Property Type Specifiers

MAPI Property Type Hex Value OLE Variant Type Description
PT_BINARY 0102 VT_BLOB Binary (unknown format)
PT_CURRENCY 0006 VT_CY 8-byte integer (scaled by 10,000)
PT_DOUBLE 0005 VT_R8 8-byte real (floating point)
PT_ERROR 000A VT_ERROR SCODE value; 32-bit unsigned integer
PT_FLOAT 0004 VT_R4 4-byte real (floating point)
PT_LONG 0003 VT_I4 4-byte integer
PT_NULL 0001 VT_NULL Null (no valid data)
PT_SHORT 0002 VT_I2 2-byte integer
PT_STRING8 001E or 001F VT_BSTR String
PT_SYSTIME 0040 VT_DATE 8-byte real (date in integer, time in fraction)

See Also