SPWeb.GetChanges method (SPChangeQuery)

Gets changes from the change log filtered by the specified query.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

public SPChangeCollection GetChanges(
	SPChangeQuery query
)

Parameters

query
Type: Microsoft.SharePoint.SPChangeQuery

The query that is performed against the change log.

Return value

Type: Microsoft.SharePoint.SPChangeCollection
The changes that have occurred on the website based on the specified query. Specify the maximum number of changes to return by setting the FetchLimit property of the query parameter.

Use this method to get changes to particular objects, rather than all objects, or only for selected actions on particular objects. For more information, see the the SPChangeQuery class.

NoteNote

By default, the change log retains data for 60 days. To change the default retention period, set the ChangeLogRetentionPeriod property.

The following example is a console application that creates a text file with information about list items that have been added, deleted, or updated. The application calls the GetChanges method in a loop, fetching changes in batches until all changes have been retrieved.

using System;
using System.IO;
using Microsoft.SharePoint;

namespace Test
{
   class ConsoleApp
   {
      static void Main(string[] args)
      {
         using (SPSite siteCollection = new SPSite("http://localhost"))
         {
            using (SPWeb webSite = siteCollection.RootWeb)
            {

               // Construct a query
               SPChangeQuery query = new SPChangeQuery(false,  // limit object types
                                                       false); // limit change types

               // object type 
               query.Item = true;

               // change types 
               query.Add = true;
               query.Delete = true;
               query.Update = true;

               SPTimeZone timeZone = webSite.RegionalSettings.TimeZone;
               long total = 0;

               string fileName = "ItemChanges.txt";
               StreamWriter writer = File.AppendText(fileName);

               while (true)
               {

                  SPChangeCollection changes = webSite.GetChanges(query);
                  total += changes.Count;

                  foreach (SPChangeItem change in changes)
                  {
                     // Get the list title
                     string listTitle = String.Empty;
                     string itemName = String.Empty;
                     SPList list = null;
                     try
                     {
                        list = webSite.Lists[change.ListId];
                        listTitle = list.Title;
                     }
                     catch (SPException)
                     {
                        listTitle = "Unknown";
                     }

                     // Get the item title
                     if (list != null)
                     {
                        SPListItem item = null;
                        try
                        {
                           item = list.GetItemByUniqueId(change.UniqueId);
                           itemName = item.Name;
                        }
                        catch (ArgumentException)
                        {
                           itemName = "Unknown";
                        }

                     }

                     // Write to the log
                     writer.WriteLine("\r\nDate: {0}",
                        timeZone.UTCToLocalTime(change.Time).ToString());
                     writer.WriteLine("Change: {0} item", change.ChangeType);
                     writer.WriteLine("List: {0}", listTitle);
                     writer.WriteLine("Item: {0}", itemName);

                  }

                  // Break out of loop if we have the last batch
                  if (changes.Count < query.FetchLimit)
                     break;
                  // Otherwise, go get another batch
                  query.ChangeTokenStart = changes.LastChangeToken;
               }

               writer.WriteLine("\r\nTotal changes = {0:#,#}", total);
               writer.Flush();
               writer.Close();

               Console.WriteLine("{0} changes written to {1}", total, fileName);
            }
         }
         Console.Write("\nPress ENTER to continue...");
         Console.ReadLine();
      }
   }
}
Show: