Directory Services

Polling for Changes Using USNChanged

The DirSync control is powerful and efficient, but has two significant limitations:

Changes from Active Directory can also be obtained by querying the uSNChanged attribute, which avoids the limitations of the DirSync control. This alternative is not better than the DirSync control in all respects because it involves transmitting all attributes when any attribute changes and it requires more work from the application developer to handle certain failure scenarios correctly. It is, currently, the best way to write certain change-tracking applications.

When a domain controller modifies an object it sets that object's uSNChanged attribute to a value that is larger than the previous value of the uSNChanged attribute for that object, and larger than the current value of the uSNChanged attribute for all other objects held on that domain controller. As a consequence, an application can find the most recently changed object on a domain controller by finding the object with the largest uSNChanged value. The second most recently changed object on a domain controller will have the second largest uSNChanged value, and so on.

The uSNChanged attribute is not replicated, therefore reading an object's uSNChanged attribute at two different domain controllers will typically give different values.

For example, the uSNChanged attribute can be used to track changes in a subtree S. First, perform a "full sync" of the subtree S. Suppose the largest uSNChanged value for any object in S is U. Periodically query for all objects in subtree S whose uSNChanged value is greater than U. The query will return all objects that have changed since the full sync. Set U to the largest uSNChanged among these changed objects, and you are ready to poll again.

The subtleties of implementing a uSNChanged synchronization application include:

For more information, and a code example that shows the basics of a USNChanged synchronization application, see Example Code to Retrieve Changes Using USNChanged.