Platform SDK: Active Directory, ADSI, and Directory Services

Polling for Changes Using the DirSync Control

The directory synchronization (DirSync) control is an LDAP server extension that enables an application to search an Active Directory™ partition for objects that have changed since a previous state.

Note that the DirSync caller must have the SE_SYNC_AGENT_NAME privilege, which enables the caller to read all objects and attributes in Active Directory, regardless of the access protections on the objects and attributes. By default, this privilege is assigned to the Administrator and LocalSystem accounts on domain controllers. If your application needs to run under an account that does not have this privilege, use the change-tracking technique described in Polling for Changes Using USNChanged. For more information about privileges, see Privileges.

You can use the DirSync control through ADSI by specifying the ADS_SEARCHPREF_DIRSYNC search preference when using IDirectorySearch. For sample code, see Example Code Using ADS_SEARCHPREF_DIRSYNC. You can also perform a DirSync search using the LDAP API. The following discussion describes the ADSI implementation, most of which also applies to using LDAP directly, except as noted at the end of this topic.

When you perform a DirSync search, you pass in a provider-specific blob of data (a cookie) that identifies the directory's state at the time of the previous DirSync search. For the first search, you pass in a NULL cookie, and the search returns all objects that match the filter. The search also returns a valid cookie. Store the cookie in the same storage that you are synchronizing with Active Directory. On subsequent searches, retrieve the cookie from storage and pass it with the search request. The search results now include only the objects and attributes that have changed since the previous state identified by the cookie. The search also returns a new cookie to store for the next search.

The client's search request also specifies the following search parameters:

Base of the search
The base of a DirSync search must be the root of a directory partition, which can be a domain partition, the configuration partition, or the schema partition.
Scope
The scope of a DirSync search must be ADS_SCOPE_SUBTREE, that is, the entire subtree of the partition. Note that for a search of a domain partition, the subtree includes the heads (but not the contents) of the configuration and schema partitions. To poll for changes in a smaller scope, use the USNChanged technique instead of DirSync.
Filter
You can specify any valid search filter. For an initial search with a NULL cookie, the results include all objects that match the filter. For subsequent searches with a valid cookie, the search results include information only for objects that match the filter and have changed since the state indicated by the cookie. For information on search filters, see Creating a Query Filter.
Attributes
You can specify a list of attributes to be returned when a change occurs. For each object, the initial results include all the requested attributes that are set on the object. Subsequent search results include only the specified attributes that have changed. Attributes that have not changed are not included in the search results. In the ADSI implementation, the search results automatically include the ADsPath binding string and objectGUID of each object. Also, the specified attributes act as an additional filter; the initial search results include only objects that have at least one of the specified attributes set.

A few additional details:

LDAP Implementation of the DirSync Control

You can also perform a DirSync search by specifying the LDAP_SERVER_DIRSYNC_OID control with one of the LDAP search functions, such as ldap_search_ext. If you use the LDAP API, be sure to also specify the LDAP_SERVER_EXTENDED_DN_OID and LDAP_SERVER_SHOW_DELETED_OID controls. The EXTENDED_DN control causes an LDAP search to return an extended form of the distinguished name that includes the objectGUID (and objectSID for security principal objects such as users, groups, and computers.) The SHOW_DELETED control causes the search results to include information for deleted objects. Note that these controls are automatically included in the ADSI implementation.