Each entry in the change log is represented by an SPChange object. When a change is logged, it is stamped with an identifying token, which is represented by an SPChangeToken object in the SPChange object’s ChangeToken property.
You can get the change token that will be used to mark the next change that is logged for the list, site, site collection, or content database that you are programming against by accessing the CurrentChangeToken property of the SPList, SPWeb, SPSite, or SPContentDatabase class.
More often, you will want to use a change token to limit the scope of a query against the change log. For example, you can retrieve changes starting from a particular point in the change log by passing an SPChangeToken object as an argument to the GetChanges method of the SPList, SPWeb, SPSite, or SPContentDatabase class. The change token that you pass represents the sequential location in the log where you want your query to begin.
You can also call the GetChanges method with two change tokens as arguments, indicating both a starting and an ending point for your query. Similarly, you can specify start and end points by calling another overload of GetChanges, this one accepting an instance of the SPChangeQuery class. This class has properties that you can use to refine a query, including two properties that hold SPChangeToken objects, the ChangeTokenStart property and the ChangeTokenEnd property.
All overloads of the GetChanges method return an SPChangeCollection object. For performance reasons, the maximum number of changes that are returned in a single collection is limited to 1000, a value that is specified by the CountLimit constant. If the scope of your query is likely to include more than 1000 changes, you can call the GetChanges method in a loop to retrieve changes in batches. When you do this, use the token that is returned by the LastChangeToken property of the first batch to get the second batch, and so on until you get a batch with fewer than 1000 changes, which signifies that you have reached the end of the list. The general approach is illustrated by the following code, which retrieves all changes logged for a site collection.
' Start at the beginning of the log.
Dim token As SPChangeToken = Nothing
' Loop until we reach the end of the log.
While True
' Get a batch of changes.
Dim changes As SPChangeCollection = siteCollection.GetChanges(token)
Dim change As SPChange
For Each change in changes
' Process the change.
Next change
' Break out of the loop when we fetch the last batch.
If changes.Count < SPChangeCollection.CountLimit Then
Exit While
End If
' Go get another batch of changes starting where we left off.
token = changes.LastChangeToken
End While
// Start at the beginning of the log.
SPChangeToken token = null;
// Loop until we reach the end of the log.
while (true)
{
// Get a batch of changes.
SPChangeCollection changes = siteCollection.GetChanges(token);
foreach (SPChange change in changes)
{
// Process the change.
}
// Break out of loop if we have the last batch.
if (changes.Count < SPChangeCollection.CountLimit)
break;
// Go get another batch of changes starting where we left off.
token = changes.LastChangeToken;
}