SPWeb.GetChanges method

Gets all of changes listed in the current change log for the website.

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

Syntax

'Declaration
Public Function GetChanges As SPChangeCollection
'Usage
Dim instance As SPWeb
Dim returnValue As SPChangeCollection

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

Return value

Type: Microsoft.SharePoint.SPChangeCollection
The changes.

Remarks

The total number of changes returned by a query against the change log can be very large. For performance reasons, changes are returned in batches of up to 2000. This overload method returns only the first batch of changes recorded in the log.

To get all of changes rather than only the first batch, call this method in a loop until it returns a collection with 0 changes, signifying that it has reached the end of the log. Use the ChangeToken property from the last change of the first batch to get the second batch, and so on, until you get an empty collection. For an example, see the GetChanges(SPChangeToken) method.

Note

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

Examples

The example is a console application that uses the GetChanges method to create a log file containing information about the first batch of changes listed in the change log.

using System;
using System.IO;
using Microsoft.SharePoint;

namespace Test
{
   class ConsoleApp
   {
      static void Main(string[] args)
      {
         using (SPSite siteCollection = new SPSite("https://localhost"))
         {
            using (SPWeb webSite = siteCollection.RootWeb)
            {
               SPTimeZone timeZone = webSite.RegionalSettings.TimeZone;
               string fileName = "ChangeLog.txt";
               StreamWriter writer = File.AppendText(fileName);

               SPChangeCollection changes = webSite.GetChanges();

               foreach (SPChange change in changes)
               {
                  writer.WriteLine( "\r\nDate: {0}", timeZone.UTCToLocalTime(change.Time).ToString());
                  writer.WriteLine("Object type: {0}", change.GetType().ToString());
                  writer.WriteLine("Change type: {0}", change.ChangeType);
               }

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

               Console.WriteLine("{0} changes written to {1}", changes.Count, fileName);
            }
         }
         Console.Write("\nPress ENTER to continue...");
         Console.ReadLine();
      }
   }
}
Imports System
Imports System.IO
Imports Microsoft.SharePoint

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

            Dim timeZone As SPTimeZone = webSite.RegionalSettings.TimeZone
            Dim fileName As String = "ChangeLog.txt"
            Dim writer As StreamWriter = File.AppendText(fileName)

            Dim changes As SPChangeCollection = webSite.GetChanges()

            For Each change As SPChange In changes
               writer.WriteLine(vbCrLf + "Date: {0}", timeZone.UTCToLocalTime(change.Time).ToString())
               writer.WriteLine("Object type: {0}", change.GetType().ToString())
               writer.WriteLine("Change type: {0}", change.ChangeType)
            Next change

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

            Console.WriteLine("{0} changes written to {1}", changes.Count, fileName)
         End Using
      End Using
      Console.Write(vbCrLf + "Press ENTER to continue...")
      Console.ReadLine()
   End Sub
End Module

See also

Reference

SPWeb class

SPWeb members

GetChanges overload

Microsoft.SharePoint namespace

Other resources

Using the Change Log