다음을 통해 공유


Querying for Specific Changes

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

To query for specific change types to a specific type of object, you must set the object and the corresponding event(s) of the SPChangeQuery class. Therefore, to query for the addition of any user to a Web site, set the User and Add properties to true. Setting only User to true does not retrieve all user changes. To query for all types of changes to user objects, you must set the User property and all the possible event properties to true (in other words, Add, Delete, and Update).

Example

The following code example shows how to query for changes to a particular type of object. In this case, all SPUser changes are queried at the content database scope. In the call to the GetChanges method, an SPChangeQuery object is passed that queries for all users added at the database scope.

Dim database As SPContentDatabase = New SPSite("http://siteUrl").ContentDatabase

' Creating a change token using the time stamp.
Dim startTime As DateTime = DateTime.Now.Subtract(TimeSpan.FromDays(1))
Dim startToken As New SPChangeToken(SPChangeCollection.CollectionScope.ContentDB, database.Id, startTime)
Dim changeQuery As New SPChangeQuery(False, False)
changeQuery.User = True
changeQuery.Add = True
changeQuery.ChangeTokenStart = startToken

' Querying for changes at a later time.
changeQuery.ChangeTokenEnd = database.CurrentChangeToken
Dim changes As SPChangeCollection = database.GetChanges(changeQuery)
Dim change As SPChange

For Each change In changes
    Dim userChange As SPChangeUser = CType(change, SPChangeUser)
    ' Use the SPChangeUser properties e.g. Id to retrieve 
    ' the SPUser object if needed.
Next change
SPContentDatabase database = new SPSite("http://siteUrl").ContentDatabase;

/* Creating a change token using the time stamp.*/
DateTime startTime = DateTime.Now.Subtract(TimeSpan.FromDays(1));
SPChangeToken startToken = new SPChangeToken(SPChangeCollection.CollectionScope.ContentDB, database.Id, startTime);
SPChangeQuery changeQuery = new SPChangeQuery(false, false);
changeQuery.User = true;
changeQuery.Add = true;
changeQuery.ChangeTokenStart = startToken;

/* Querying for changes at a later time.*/
changeQuery.ChangeTokenEnd = database.CurrentChangeToken;
SPChangeCollection changes = database.GetChanges(changeQuery);
foreach (SPChange change in changes)
{
    SPChangeUser userChange = (SPChangeUser)change;
    /* Use the SPChangeUser properties (e.g. Id) to retrieve 
    the SPUser object if needed.*/
}