Export (0) Print
Expand All

SPList.GetChanges method (SPChangeQuery)

Gets the changes to the list from the change log as filtered by the specified query.

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

'Declaration
Public Function GetChanges ( _
	query As SPChangeQuery _
) As SPChangeCollection
'Usage
Dim instance As SPList
Dim query As SPChangeQuery
Dim returnValue As SPChangeCollection

returnValue = instance.GetChanges(query)

Parameters

query
Type: Microsoft.SharePoint.SPChangeQuery

The query.

Return value

Type: Microsoft.SharePoint.SPChangeCollection
The changes recorded for the list, filtered by the specified query. The number of changes that can be returned in a single collection is limited for performance reasons. You can adjust the maximum size of the collection by setting the FetchLimit property.

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 about queries, see the SPChangeQuery class.

NoteNote

By default, the change log retains data for 60 days. You can configure the retention period by setting the ChangeLogRetentionPeriod property.

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)

            ' Specify the object type. 
            query.Item = True

            ' Specify 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.GetItemByUniqueId(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 < query.FetchLimit 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
Show:
© 2015 Microsoft