SPChangeCollection Class

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

Inheritance Hierarchy

System.Object
  Microsoft.SharePoint.Administration.SPAutoSerializingObject
    Microsoft.SharePoint.SPBaseCollection
      Microsoft.SharePoint.SPChangeCollection

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

Syntax

<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel := True)> _
<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel := True)> _
Public NotInheritable Class SPChangeCollection _
    Inherits SPBaseCollection

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

Remarks

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().

Examples

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.

Imports System
Imports System.IO
Imports Microsoft.SharePoint

Module ConsoleApp
   Sub Main()
      Using siteCollection As SPSite = New SPSite("http://localhost")
         Using webSite As SPWeb = siteCollection.RootWeb

            ' Construct a query
            Dim query As New SPChangeQuery(False, False)

            ' object type
            query.List = True

            ' change types 
            query.Add = True
            query.Delete = True
            query.Update = True

            Dim timeZone As SPTimeZone = webSite.RegionalSettings.TimeZone
            Dim total As Long = 0

            Dim fileName As String = "C:\ListChanges.txt"
            Dim writer As StreamWriter = File.AppendText(fileName)

            While True

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

               For Each change As SPChangeList In changes
                  ' Get the list title
                  Dim listTitle As String = String.Empty
                  Dim itemName As String = String.Empty
                  Dim list As SPList = Nothing
                  Try
                     list = webSite.Lists(change.Id)
                     listTitle = list.Title
                  Catch ex As SPException
                     listTitle = "Unknown"
                  End Try

                  ' Write to the log
                  writer.WriteLine(vbCrLf + "Date: {0}", _
                     timeZone.UTCToLocalTime(change.Time).ToString())
                  writer.WriteLine("Change: {0} list", change.ChangeType)
                  writer.WriteLine("List: {0}", listTitle)
               Next change

               ' Break out of the loop when we fetch the last batch of changes
               If changes.Count < SPChangeCollection.CountLimit Then
                  Exit While
               End If

               ' Go get another batch of changes starting where we left off
               query.ChangeTokenStart = changes.LastChangeToken

            End While

            writer.WriteLine(vbCrLf + "Total changes = {0:#,#}", total)
            writer.Flush()
            writer.Close()

            Console.WriteLine("{0} changes written to {1}", total, fileName)

         End Using
      End Using

      Console.Write(vbCrLf + "Press ENTER to continue...")
      Console.ReadLine()

   End Sub
End Module
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();
      }
   }
}

Thread Safety

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

See Also

Reference

SPChangeCollection Members

Microsoft.SharePoint Namespace

Other Resources

Event Logs in Windows SharePoint Services