SPChangeTokenCollection 类

表示SPChangeToken对象的集合。

继承层次结构

System.Object
  Microsoft.SharePoint.Administration.SPAutoSerializingObject
    Microsoft.SharePoint.SPBaseCollection
      Microsoft.SharePoint.SPChangeTokenCollection

命名空间:  Microsoft.SharePoint
程序集:  Microsoft.SharePoint(位于 Microsoft.SharePoint.dll 中)

语法

声明
Public NotInheritable Class SPChangeTokenCollection _
    Inherits SPBaseCollection
用法
Dim instance As SPChangeTokenCollection
public sealed class SPChangeTokenCollection : SPBaseCollection

备注

此集合提供,以便您可以轻松地管理 Web 应用程序级别的更改日志。在 Web 应用程序中的每个内容数据库维护它自己的更改日志。如果要对多个内容数据库统一的更改报表,可以使用SPChangeTokenCollection对象来存储单个集合中的所有数据库更改标记。一旦更改标记的集合,可以枚举集合并为其更改反过来要求每个数据库,通过调用GetChanges(SPChangeToken)方法。此外可以序列化集合使用ToString()方法,并将其存储在磁盘上。以后,当您想要重新访问更改日志,您可以重新构造集合并使用另一种形式的构造函数接受一个字符串参数,并将更改记录再次查询的反序列化的更改标记集合。

示例

下面的示例是一个控制台应用程序查询的 Web 应用程序中的每个内容数据库的更改日志。在程序的第一次运行,从每个日志中检索的所有更改。在处理更改的每个集合之后, 程序保存到一个SPChangeTokenCollection集合更改集合中的最后一次更改标记。当处理完所有日志时, SPChangeTokenCollection对象进行序列化,并生成的字符串会存储在磁盘上的文件中。

以后运行该程序,读取的数据文件,检索集合的字符串表示形式时,重建的SPChangeTokenCollection对象。集合中的更改标记随后用于提取自上一次运行该程序所做的更改。

本示例将不会尝试将它从更改日志中检索的信息保存。它只需打印输出到控制台。有用的增强功能就是整合文件或数据库中的更改为添加代码,它们可能会受到进一步的分析。

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

namespace Test
{
   class ConsoleApp
   {
      private const string DATA_FILE_PATH = "ChangeTokens.dat";

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

         // Get a collection of change tokens
         SPChangeTokenCollection tokens = GetTokens();

         // Process the changes for each database
         foreach (SPContentDatabase db in dbs)
         {
            // Get an Id for the current database
            SPPersistedObject o = db as SPPersistedObject;
            Guid id = o.Id;
            Console.WriteLine("\nContent database ID = {0}", id.ToString());

            // Create a query.
            SPChangeQuery query = new SPChangeQuery(true, true);

            /* Get the starting token. 
             * Note that if the token is not
             * found, the indexer returns null.
             * Passing a null token fetches changes
             * from the beginning of the log.
             */
            query.ChangeTokenStart = tokens[id];

            while (true)
            {
               // Get a batch of changes
               SPChangeCollection changes = db.GetChanges(query);

               // Process them
               foreach (SPChange change in changes)
               {
                  Console.WriteLine("Date: {0}  Type of object: {1}  Type of change: {2}",
                     change.Time.ToShortDateString(), change.GetType().ToString(), change.ChangeType);
               }

               // If this is the last batch, exit
               if (changes.Count < query.FetchLimit)
               {
                  // Save the last token as a starting point for the next run
                  tokens.Add(changes.LastChangeToken, true);
                  break;
               }
               else
               {                
                  // Starting point for next batch
                  query.ChangeTokenStart = changes.LastChangeToken;
               }
            }
         }

         // Persist the token collection
         SaveTokens(tokens);

         Console.Write("\nPress ENTER to continue...");
         Console.ReadLine();
      }

      static SPChangeTokenCollection GetTokens()
      {
         SPChangeTokenCollection tokens = new SPChangeTokenCollection();

         // If we have tokens from the last run, use them
         if (File.Exists(DATA_FILE_PATH))
         {
            using (FileStream fs = File.OpenRead(DATA_FILE_PATH))
            {
               BinaryReader br = new BinaryReader(fs);
               try
               {
                  string s = br.ReadString();
                  // Construct a change token from string
                  tokens = new SPChangeTokenCollection(s);
               }
               catch (EndOfStreamException e)
               {
                  // No serialized string, so do nothing
               }
               finally
               {
                  br.Close();
               }
            }
         }

         return tokens;
      }

      static void SaveTokens(SPChangeTokenCollection tokens)
      {
         using (FileStream fs = File.Create(DATA_FILE_PATH))
         {
            // Serialize the tokens
            BinaryWriter bw = new BinaryWriter(fs);
            string s = tokens.ToString();
            bw.Write(s);

            // flush and close
            bw.Flush();
            bw.Close();
         }
      }
   }
}
Imports System
Imports System.IO
Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.Administration

Module ConsoleApp

   Private Const DATA_FILE_PATH As String = "ChangeTokens.dat"

   Sub Main()

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

      ' Get a collection of change tokens
      Dim tokens As SPChangeTokenCollection = GetTokens()

      ' Process the changes for each database
      Dim db As SPContentDatabase
      For Each db In dbs
         ' Get an Id for the current database
         Dim o As SPPersistedObject = CType(db, SPPersistedObject)
         Dim id As Guid = o.Id
         Console.WriteLine(vbCrLf + "Content database ID = {0}", id.ToString())

         ' Create a query
         Dim query As New SPChangeQuery(True, True)

         ' Get the starting token. 
         ' Note that if the token is not
         ' found, the indexer returns a null value.
         ' Passing a null token fetches changes
         ' from the beginning of the log.
         query.ChangeTokenStart = tokens(id)

         While (True)
            ' Get a batch of changes
            Dim changes As SPChangeCollection = db.GetChanges(query)

            ' Process them
            Dim change As SPChange
            For Each change In changes
               Console.WriteLine("Date: {0}  Type of object: {1}  Type of change: {2}", _
                     change.Time.ToShortDateString(), change.GetType().ToString(), change.ChangeType)
            Next change

            ' If this is the last batch, exit
            If changes.Count < query.FetchLimit Then
               ' Save the last token as a starting point for the next run
               tokens.Add(changes.LastChangeToken, True)
               Exit While
            Else
               ' Starting point for next batch
               query.ChangeTokenStart = changes.LastChangeToken
            End If

         End While

      Next db

      'Persist the token collection
      SaveTokens(tokens)

      Console.Write(vbCrLf + "Press ENTER to continue...")
      Console.ReadLine()

   End Sub

   Function GetTokens() As SPChangeTokenCollection

      Dim tokens As SPChangeTokenCollection = New SPChangeTokenCollection()

      ' If we have tokens from the last run, use them
      If File.Exists(DATA_FILE_PATH) Then
         Using fs As FileStream = File.OpenRead(DATA_FILE_PATH)
            Dim br As BinaryReader = New BinaryReader(fs)
            Try
               Dim s As String = br.ReadString()
               ' Construct a change token from string
               tokens = New SPChangeTokenCollection(s)
            Catch e As EndOfStreamException
               ' No serialized string, so do nothing
            Finally
               br.Close()
            End Try
         End Using
      End If

      Return tokens
   End Function

   Sub SaveTokens(ByRef tokens As SPChangeTokenCollection)
      Using fs As FileStream = File.Create(DATA_FILE_PATH)
         ' Serialize the tokens
         Dim bw As BinaryWriter = New BinaryWriter(fs)
         Dim s As String = tokens.ToString()
         bw.Write(s)

         ' flush and close
         bw.Flush()
         bw.Close()
      End Using
   End Sub

End Module

线程安全性

该类型的任何公共 静态 (已共享 在 Visual Basic 中) 成员都是线程安全的。不保证任何实例成员都是线程安全的。

另请参阅

引用

SPChangeTokenCollection 成员

Microsoft.SharePoint 命名空间

ContentDatabases

SPContentDatabase

GetChanges(SPChangeToken)