Share via


SPContentDatabase.GetChanges method (SPChangeToken, SPChangeToken)

Returns a collection of changes that have been logged over a specified period of time.

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

Syntax

'Declaration
Public Function GetChanges ( _
    changeToken As SPChangeToken, _
    changeTokenEnd As SPChangeToken _
) As SPChangeCollection
'Usage
Dim instance As SPContentDatabase
Dim changeToken As SPChangeToken
Dim changeTokenEnd As SPChangeToken
Dim returnValue As SPChangeCollection

returnValue = instance.GetChanges(changeToken, _
    changeTokenEnd)
public SPChangeCollection GetChanges(
    SPChangeToken changeToken,
    SPChangeToken changeTokenEnd
)

Parameters

Return value

Type: Microsoft.SharePoint.SPChangeCollection
A collection of SPChange objects that represent the changes.

Remarks

The following rules apply to the change tokens that are passed as arguments.

  • If either token refers to a time before the start of the current change log, the method throws an SPException exception.

  • If the time specified by the second token is earlier than the time specified by the first token, the method returns an empty collection.

  • If the first token is a null reference (Nothing in Visual Basic), the change collection that is returned starts at the beginning of the current change log.

  • If the second token is a null reference (Nothing in Visual Basic), the change collection that is returned includes all changes after the date specified by the first change token, up to the limit for a single collection. If more changes occurred in this period, the first batch is returned.

Note

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

Examples

The following example is a console application that queries the change log for changes that took place during a period of seven days. The application then prints information about each change to the console.

using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace Test
{
   class ConsoleApp
   {
      static void Main(string[] args)
      {
         using (SPSite siteCollection = new SPSite("https://localhost"))
         {
            using (SPWeb webSite = siteCollection.RootWeb)
            {
               // We need to do this to get an Id. SPContentDatabase.ID property is obsolete.
               SPPersistedObject db = (SPPersistedObject)siteCollection.ContentDatabase;

               SPChangeToken startToken = new SPChangeToken(
                  SPChangeCollection.CollectionScope.ContentDB,
                  db.Id,
                  new DateTime(2008, 11, 17));

               SPChangeToken endToken = new SPChangeToken(
                  SPChangeCollection.CollectionScope.ContentDB,
                  db.Id,
                  new DateTime(2008, 11, 23));

               long total = 0;

               SPChangeCollection changes = 
                  siteCollection.ContentDatabase.GetChanges(startToken, endToken);
               while (changes.Count > 0)
               {
                  total += changes.Count;

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

                  startToken = changes.LastChangeToken;
                  changes = siteCollection.ContentDatabase.GetChanges(startToken, endToken);
               }

               Console.WriteLine("\nTotal changes = {0:#,#}", total);
            }
         }
         Console.Write("\nPress ENTER to continue...");
         Console.ReadLine();
      }
   }
}
Imports System
Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.Administration

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

            ' We need to do this to get an Id. SPContentDatabase.ID property is obsolete.
            Dim db As SPPersistedObject = CType(siteCollection.ContentDatabase, SPPersistedObject)

            Dim startToken As New SPChangeToken(SPChangeCollection.CollectionScope.ContentDB, _
                                                db.Id, _
                                                New DateTime(2008, 11, 17))

            Dim endToken As New SPChangeToken(SPChangeCollection.CollectionScope.ContentDB, _
                                              db.Id, _
                                              New DateTime(2008, 11, 23))

            Dim total As Long = 0

            Dim changes As SPChangeCollection = _
               siteCollection.ContentDatabase.GetChanges(startToken, endToken)
            While changes.Count > 0
               total += changes.Count

               For Each change As SPChange In changes

                  Console.WriteLine(vbCrLf + "Date: {0}", change.Time.ToShortDateString())
                  Console.WriteLine("Object type: {0}", change.GetType().ToString())
                  Console.WriteLine("Change type: {0}", change.ChangeType)

               Next change

               startToken = changes.LastChangeToken
               changes = siteCollection.ContentDatabase.GetChanges(startToken, endToken)
            End While

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

         End Using
      End Using
      Console.Write(vbCrLf + "Press ENTER to continue...")
      Console.ReadLine()
   End Sub
End Module

See also

Reference

SPContentDatabase class

SPContentDatabase members

GetChanges overload

Microsoft.SharePoint.Administration namespace

Other resources

Using the Change Log