SharePoint Blogs / SharePoint University
SharePoint Blogs and SharePoint University - all in one place!
Need SharePoint Training? Attend a SharePoint Bootcamp!

Please delete cookies related to sharepointblogs.com and sharepointu.com to resolve login issues!

Querying the SharePoint Change Logs

SharePoint 2007 keeps a record of all changes made to data, users, permissions, sites etc in a Change Log which you can access via the object model. This is actually how the indexer figures out what to crawl as part of an incremental crawl.

You need to be aware that the log history length is controlled by configuration available to server admins so it is possible that the changes you are looking for might have dropped off the log. And this is why there is a caveat around incremental crawling in Indexing...

For example, say I wanted to get all the items that have been added, modified or deleted from a list:

  //Create a new query, and set it not to return anything by default.
 SPChangeQuery listQuery = new SPChangeQuery(false, false);

 //Now set if to return the types of info we want.
 //In this case item changes of type Add, Delete and Update...
 listQuery.Item = true;
 listQuery.Add = true;
 listQuery.Delete = true;
 listQuery.Update = true;

 //Get the changes that match the query.
 SPChangeCollection changesCollection = currentWeb.GetChanges(listQuery);

 //Do something with them.
 foreach (SPChangeItem currentChange in changesCollection)
  {
     ...
   }

The code above will return ALL changes in the log for list items in the specified Web. A Change Token is returned in the as part of the SPChangeCollection and can be passed to subsequent calls to GetChanges to return changes since the last time you checked. Meaning you can perform synchronisation operations in your own apps.

To get the last change token:

    SPChangeToken lastChangeToken = changesCollection.LastChangeToken;

Set the queries start token to the last change token we had:

    listQuery.ChangeTokenStart = lastChangeToken;

And then call the GetChanges method as above.

 


Posted 03-22-2007 12:15 AM by adrh

Comments

PeterC wrote re: Querying the SharePoint Change Logs
on 10-29-2008 1:30 PM

Question the SPChangeCollection  object has a limit of 1000 objects, what do you approach would you suggest if the more than 1000 items are expected to be changed.

Need SharePoint Training? Attend a SharePoint Bootcamp!
Posts (c) their respective authors. Everything else (c) 2009 SharePoint Experts, Inc.