This documentation is archived and is not being maintained.

SPChangeCollection Class

Windows SharePoint Services 3

Represents a collection of SPChange objects. The maximum number of changes that can be represented in a collection is 1000.

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

[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel = true)]
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel = true)]
public sealed class SPChangeCollection : SPBaseCollection

Use the GetChanges method of the SPList, SPWeb, SPSite, or SPContentDatabase object to return the collection of changes that have occurred in the given scope.

The maximum number of changes that an SPChangeCollection object can contain is specified by the constant CountLimit().

The following example is a console application that queries the change log for changes to lists in the root Web site of a site collection. After the changes are retrieved, the application examines each change and writes information about it to a text file. Note that the application calls the GetChanges method in a loop, retrieving changes in batches until the end of the change log is reached.

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.List = true;

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

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

               string fileName = "C:\\ListChanges.txt";
               StreamWriter writer = File.AppendText(fileName);

               while (true)
               {

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

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

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

                  }

                  // Break out of loop if we have the last batch
                  if (changes.Count < SPChangeCollection.CountLimit)
                     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();
      }
   }
}

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show: