This documentation is archived and is not being maintained.

SPList.GetChanges Method (SPChangeToken, SPChangeToken)

Windows SharePoint Services 3

Returns a collection of changes logged over a specified period of time.

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

public SPChangeCollection GetChanges(
	SPChangeToken changeToken,
	SPChangeToken changeTokenEnd
)

Parameters

changeToken
Type: Microsoft.SharePoint.SPChangeToken
An SPChangeToken object that specifies a starting date and time.
changeTokenEnd
Type: Microsoft.SharePoint.SPChangeToken
An SPChangeToken object that specifies an ending date and time.

Return Value

Type: Microsoft.SharePoint.SPChangeCollection
An SPChangeCollection object that represents the changes.

When you construct the SPChangeToken objects to use with this method, pass SPChangeCollection.CollectionScope.List as the constructor’s first argument, the value of the current object’s SPList.ID property as the second argument, and a DateTime object as the third argument.

In addition, the following rules apply:

  • If either token refers to a time before the start of the current change log, the method throws an SPException exception.

  • If the time of the second token is before the time of the first token, the method returns an empty collection.

  • If the first token is a null reference (Nothing in Visual Basic), the change collection that is returned starts at the beginning of the current change log.

  • If the second token is a null reference (Nothing in Visual Basic), the change collection that is returned includes all changes after the date specified by the first change token. If more than 1000 changes occurred in this period, the first 1000 are returned.

NoteNote

By default, the change log retains data for 15 days. You can configure this retention setting in Windows SharePoint Services by using the Web Application General Settings page. To reach this page, open the Central Administration site, then click the Application Management tab. In the Web Application Management group, click the link for Web application general settings.

The following example is a console application that queries the change log for changes to a list during a period of seven days. After retrieving the changes, the application prints information about each change to the console.

using System;
using Microsoft.SharePoint;

namespace Test
{
   class ConsoleApp
   {
      static void Main(string[] args)
      {
         using (SPSite siteCollection = new SPSite("http://localhost"))
         {
            using (SPWeb webSite = siteCollection.OpenWeb())
            {
               // Get a list.
               SPList list = webSite.Lists[0];

               SPChangeToken startToken = new SPChangeToken(
                  SPChangeCollection.CollectionScope.List,
                  list.ID,
                  new DateTime(2008, 10, 12));

               SPChangeToken endToken = new SPChangeToken(
                  SPChangeCollection.CollectionScope.List,
                  list.ID,
                  new DateTime(2008, 10, 18));


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

               // Loop until we reach the end of the log.
               while (true)
               {
                  SPChangeCollection changes = list.GetChanges(startToken, endToken);

                  total += changes.Count;

                  // Print info about each change to the console.
                  foreach (SPChange change in changes)
                  {
                     Console.WriteLine("\nDate: {0}",
                         timeZone.UTCToLocalTime(change.Time).ToString());
                     Console.WriteLine("Object type: {0}", change.GetType().ToString());
                     Console.WriteLine("Change: {0}", change.ChangeType.ToString());
                  }

                  // Break out of loop if we have the last batch.
                  if (changes.Count < SPChangeCollection.CountLimit)
                     break;

                  // Otherwise, go get another batch.
                  startToken = changes.LastChangeToken;
               }

               Console.WriteLine("\nTotal of {0} changes to {1} list", total, list.Title);
            }
         }
         Console.Write("\nPress ENTER to continue...");
         Console.ReadLine();
      }
   }
}
Show: