SPChangeTokenCollection.Item property (Guid)

Gets the change token with the specified GUID from the collection of change token objects.

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

Syntax

'Declaration
Public ReadOnly Default Property Item ( _
    scopeId As Guid _
) As SPChangeToken
    Get
'Usage
Dim instance As SPChangeTokenCollection
Dim scopeId As Guid
Dim value As SPChangeToken

value = instance(scopeId)
public SPChangeToken this[
    Guid scopeId
] { get; }

Parameters

  • scopeId
    Type: System.Guid

    A System.Guid that identifies the change token.

Property value

Type: Microsoft.SharePoint.SPChangeToken
An SPChangeToken object that represents the change token.

Remarks

If a change token with the specified GUID does not exist in the collection, this property returns a null reference (Nothing in Visual Basic).

Each change token in a collection is indexed by the value of the token’s ScopeId property. The value of this property is identical to the value of the Id property of the target for the change token. Thus if an instance of the SPChangeTokenCollection class contains change tokens for each content database in the current Web application, you can use the GUID that identifies a particular content database as an index into the collection and get back a token that you can use to retrieve that content database’s changes.

Examples

The following example is a console application that creates a collection of content databases and a corresponding collection of change tokens. The application gets the GUID that identifies each database and uses it as an index into the collection of tokens. It then uses the token that it retrieves to query the change log for the database to which the token applies.

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

namespace Test
{
   class ConsoleApp
   {
      static void Main(string[] args)
      {
         // Get a collection of content databases
         SPWebApplication wa = SPWebApplication.Lookup(new Uri("https://localhost"));
         SPContentDatabaseCollection dbs = wa.ContentDatabases;

         // Create a collection of change tokens with
         // one token for each database. Each token marks the
         // current end point of the change log for a database.
         SPChangeTokenCollection tokens = new SPChangeTokenCollection();
         foreach (SPContentDatabase db in dbs)
            tokens.Add(db.CurrentChangeToken);

         // Do something that changes objects stored
         // within each database
         // .
         // .
         // .

         // Get any changes that have taken place
         long total = 0;
         foreach (SPContentDatabase db in dbs)
         {
            // Get a GUID for the db.
            // Note that a cast is needed to access the Id property.
            SPPersistedObject po = db as SPPersistedObject;
            Guid id = po.Id;

            // Use the GUID to get a token for the db
            SPChangeToken startingToken = tokens[id];
            
            // Get the first batch of changes 
            SPChangeCollection changes = db.GetChanges(startingToken);

            // Loop until there are no more changes 
            while (changes.Count > 0)
            { 
               // Accumulate a total
               total += changes.Count;

               // Go get another batch
               startingToken = changes.LastChangeToken;
               changes = db.GetChanges(startingToken);
            }
         }
         Console.WriteLine("Total changes: {0}", total.ToString());
         Console.Write("\nPress ENTER to continue...");
         Console.ReadLine();
      }
   }
}
Imports System
Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.Administration

Module ConsoleApp
   Sub Main()
      ' Get a collection of content databases
      Dim wa As SPWebApplication = SPWebApplication.Lookup(New Uri("https://localhost"))
      Dim dbs As SPContentDatabaseCollection = wa.ContentDatabases

      ' Create a collection of change tokens with
      ' one token for each database. Each token marks the
      ' current end point of the change log for a database.
      Dim tokens As SPChangeTokenCollection = New SPChangeTokenCollection()
      Dim db As SPContentDatabase
      For Each db In dbs
         tokens.Add(db.CurrentChangeToken)
      Next

      ' Do something that changes objects stored
      ' within each database
      ' .
      ' .
      ' .

      ' Get any changes that have taken place
      Dim total As Long = 0
      For Each db In dbs
         ' Get a GUID for the db.
         ' Note that a cast is needed to access the Id property.
         Dim po As SPPersistedObject = CType(db, SPPersistedObject)
         Dim id As Guid = po.Id

         ' Use the GUID to get a token for the db
         Dim startingToken As SPChangeToken = tokens(id)

         ' Get the first batch of changes
         Dim changes As SPChangeCollection = db.GetChanges(startingToken)

         ' Loop until there are no more changes
         While (changes.Count > 0)

            ' Accumulate a total
            total += changes.Count

            ' Go get another batch
            startingToken = changes.LastChangeToken
            changes = db.GetChanges(startingToken)
         End While
      Next db

      Console.WriteLine("Total changes: {0}", total.ToString())
      Console.Write(vbCrLf + "Press ENTER to continue...")
      Console.ReadLine()

   End Sub
End Module

See also

Reference

SPChangeTokenCollection class

SPChangeTokenCollection members

Item overload

Microsoft.SharePoint namespace

ScopeId