in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

Aaron Robertson-Hodders SharePoint Blog

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.

 

Comments

No Comments

Leave a Comment

(required )  
(optional )
(required )  
Add

About adrh

I'm a Kiwi, living in Australia (for the past 10 years). I work for MacroView, a consulting firm in Sydney specialising in SharePoint solutions, products (WISDOM Document Management extensions, Custom Search solutions) and consulting around Enterprise Search. My background includes experience developing solutions in VB, VB.Net C#, ASP, ASP.Net, SharePoint (from V1) and Office.

Need SharePoint Training? Attend a SharePoint Bootcamp!

Posts (c) their respective authors. Everything else (c) 2007 SharePoint Experts