Condividi tramite


SPWeb.GetChanges Method (SPChangeToken)

Gets the changes starting from a specified point in the change log.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online

Syntax

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

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

Parameters

Return Value

Type: Microsoft.SharePoint.SPChangeCollection
The changes that have occurred on the website since the location in the change log specified by changeToken.

Exceptions

Exception Condition
SPException

changeToken is null .

Remarks

To get an SPChangeToken object to pass as an argument to this method, extract one from the ChangeToken property of the last change returned by a previous call to this method. Or, use the SPChangeToken constructor to create a new change token.

Note

By default, the change log retains data for 60 days. To change the default retention period, set the ChangeLogRetentionPeriod property.

Examples

The following example is a console application that demonstrates how to get all changes in the log. The program loops while getting changes in batches and breaks out of the loop when it retrieves a collection with 0 members, signifying that it has reached the end of the log.

using System;
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;
               long total = 0;

               // Start with a null token so we take changes 
               // from the beginning of the log
               SPChangeToken token = null;

               // Get the first batch of changes
                SPChangeCollection changes = webSite.GetChanges(token);

               // Loop until we get zero changes
               while (changes.Count > 0)
               {
                  total += changes.Count;

                  foreach (SPChange change in changes)
                  {
                     // Process the change
                     Console.WriteLine("\nDate: {0}", timeZone.UTCToLocalTime(change.Time).ToString());
                     Console.WriteLine("Type of change: {0}", change.ChangeType.ToString());
                     Console.WriteLine("Object changed: {0}", change.GetType().ToString()); 
                  }

                  // Go get another batch
                  token = changes.LastChangeToken;
                  changes = webSite.GetChanges(token);
               }
               Console.WriteLine("\nTotal = {0:#,#} changes", total);
            }
         }
         Console.Write("\nPress ENTER to continue...");
         Console.ReadLine();
      }
   }
}
Imports System
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 total As Long = 0

            ' Start with a null token so we take changes 
            ' from the beginning of the log
            Dim token As SPChangeToken = Nothing

            ' Get the first batch of changes
            Dim changes As SPChangeCollection = webSite.GetChanges(token)

            ' Loop until we get zero changes
            While changes.Count > 0
               total += changes.Count

               For Each change As SPChange In changes
                  ' Process the change
                  Console.WriteLine(vbCrLf + "Date: {0}", timeZone.UTCToLocalTime(change.Time).ToString())
                  Console.WriteLine("Type of change: {0}", change.ChangeType.ToString())
                  Console.WriteLine("Object changed: {0}", change.GetType().ToString())
               Next change

               ' Go get another batch
               token = changes.LastChangeToken
               changes = webSite.GetChanges(token)
            End While

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

         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

CurrentChangeToken

Other Resources

Using the Change Log