SPList.GetChanges Method (SPChangeQuery)

Returns a collection of changes from the change log filtered by the specified query.

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

Syntax

Public Function GetChanges ( _
    query As SPChangeQuery _
) As SPChangeCollection

Dim instance As SPList
Dim query As SPChangeQuery
Dim returnValue As SPChangeCollection

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

Parameters

Return Value

Type: Microsoft.SharePoint.SPChangeCollection
An SPChangeCollection object that represents the changes recorded for the list, filtered by the specified query. The maximum number of changes that can be returned in a single collection is 1000.

Remarks

Use this method to filter changes when you are interested only in changes to particular objects, rather than all objects, or only in selected actions on particular objects. For more information, see the documentation for the SPChangeQuery class.

Note

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.

Examples

The following example is a simple console application that queries the change log for items added, updated or deleted from a specified list. After retrieving the changes, the application examines each change and prints the date of the change, type of change, and the name of the item that has changed to the console.

Imports System
Imports Microsoft.SharePoint

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

            ' Get a list.
            Dim list As SPList = webSite.Lists(0)

            ' 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 Integer = 0

            ' Loop until we reach the end of the log.
            While True

               Dim changes As SPChangeCollection = list.GetChanges(query)

               total += changes.Count

               ' Print info about each change to the console.
               For Each change As SPChangeItem In changes
                  ' Get the item name.
                  Dim itemName As String = String.Empty
                  Dim item As SPListItem = Nothing
                  Try
                     item = list.Items(change.UniqueId)
                     itemName = item.Name
                  Catch ex As ArgumentException
                     itemName = "Unknown"
                  End Try

                  Console.WriteLine(vbCrLf + "Date: {0}", timeZone.UTCToLocalTime(change.Time).ToString())
                  Console.WriteLine("Change: {0}", change.ChangeType)
                  Console.WriteLine("Item: {0}", itemName)

               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

            Console.WriteLine(vbCrLf + "Total of {0:#,#} changes to {1} list", total, list.Title)

         End Using
      End Using

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

   End Sub
End Module
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];

               // 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;
               int total = 0;

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

                  total += changes.Count;

                  // Print info about each change to the console.
                  foreach (SPChangeItem change in changes)
                  {
                     // Get the item name.
                     string itemName = String.Empty;
                     SPListItem item = null;
                     try
                     {
                        item = list.Items[change.UniqueId];
                        itemName = item.Name;
                     }
                     catch (ArgumentException)
                     {
                        itemName = "Unknown";
                     }

                     Console.WriteLine("\nDate: {0}",
                         timeZone.UTCToLocalTime(change.Time).ToString());
                     Console.WriteLine("Change: {0}", change.ChangeType);
                     Console.WriteLine("Item: {0}", itemName);

                  }

                  // 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;
               }

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

See Also

Reference

SPList Class

SPList Members

GetChanges Overload

Microsoft.SharePoint Namespace

Other Resources

Event Logs in Windows SharePoint Services