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 Function GetChanges ( _
    query As SPChangeQuery _
) As SPChangeCollection
Dim instance As SPWeb
Dim query As SPChangeQuery
Dim returnValue As SPChangeCollection

returnValue = instance.GetChanges(query)
public SPChangeCollection GetChanges(
    SPChangeQuery query


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.


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;
                        list = webSite.Lists[change.ListId];
                        listTitle = list.Title;
                     catch (SPException)
                        listTitle = "Unknown";

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


                     // Write to the log
                     writer.WriteLine("\r\nDate: {0}",
                     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)
                  // Otherwise, go get another batch
                  query.ChangeTokenStart = changes.LastChangeToken;

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

               Console.WriteLine("{0} changes written to {1}", total, fileName);
         Console.Write("\nPress ENTER to continue...");
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.Item = 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 = "ItemChanges.txt"
            Dim writer As StreamWriter = File.AppendText(fileName)

            While True

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

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

                  ' Get the item title
                  If list IsNot Nothing Then
                     Dim item As SPListItem = Nothing
                        item = list.GetItemByUniqueId(change.UniqueId) 
                        itemName = item.Name
                     Catch ex As ArgumentException
                        itemName = "Unknown"
                     End Try
                  End If

                  ' Write to the log
                  writer.WriteLine(vbCrLf + "Date: {0}", _
                  writer.WriteLine("Change: {0} item", change.ChangeType)
                  writer.WriteLine("List: {0}", listTitle)
                  writer.WriteLine("Item: {0}", itemName)
               Next change

               ' Break out of the loop when we fetch the last batch of changes
               If changes.Count < query.FetchLimit 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)

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

         End Using
      End Using

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

   End Sub
End Module

See also


SPWeb class

SPWeb members

GetChanges overload

Microsoft.SharePoint namespace

Other resources

Using the Change Log