共用方式為


SPChangeToken constructor (String)

初始化SPChangeToken類別,其序列化表單為基礎的新執行個體。

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

Syntax

'宣告
Public Sub New ( _
    strChangeToken As String _
)
'用途
Dim strChangeToken As String

Dim instance As New SPChangeToken(strChangeToken)
public SPChangeToken(
    string strChangeToken
)

參數

  • strChangeToken
    Type: System.String

    變更權杖序列化的形式。

Exceptions

Exception Condition
InvalidOperationException

strChangeToken並未包含正確的語彙基元。

-或-

strChangeToken所指定的版本不是 1。

備註

這個建構函式的多載適合用來重新建構被序列化,然後儲存在磁碟上的變更語彙基元。如需有關序列化SPChangeToken物件的資訊,請參閱ToString方法。

序列化的語彙基元會有五個欄位,並以分號 (;),以下列順序:

  • 版本號碼為int的字串表示。

  • 範圍,這由Scope屬性。

  • ScopeId屬性由這個範圍 ID。

  • 日期和時間變更為DateTime物件的字串表示。

  • 變更號碼為long的字串表示。

Examples

下列範例是一個主控台應用程式,查詢變更記錄的內容資料庫範圍的變更。

第一次執行應用程式,從記錄檔的開頭開始的所有變更都查詢記錄檔。這項查詢會執行第一次將SPChangeQuery物件的 [ ChangeTokenStart ] 屬性設定為 null 值,然後再將SPQuery物件傳遞至GetChanges方法。在處理所有變更之後,程式會藉由呼叫ToString()方法會序列化上次變更語彙基元,從最後的批次的變更,且將結果儲存在磁碟上的檔案。

在後續的執行,程式會還原序列化已儲存的變更語彙基元,用它來設定ChangeTokenStart屬性為其查詢與變更記錄檔]。重要的工作是在GetStartingToken函式,即稱為SPChangeToken的建構函式。

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

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

      static void Main(string[] args)
      {
         using (SPSite site = new SPSite("https://localhost"))
         {
            SPChangeQuery query = new SPChangeQuery(true, true);
            query.ChangeTokenStart = GetStartingToken();

            while (true)
            {
               // Get a batch of changes.
               SPChangeCollection changes = site.ContentDatabase.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);
               }

               // Starting point for next batch.
               query.ChangeTokenStart = changes.LastChangeToken;

               // If this is the last batch, exit.
               if (changes.Count < query.FetchLimit)
                  break;
            }
            // Serialize the last token as a starting point for the next run.
            SaveLastToken(query.ChangeTokenStart);
         }

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

      static SPChangeToken GetStartingToken()
      {
         // Passing a null token to GetChanges fetches 
         // changes from the start of the log.
         SPChangeToken token = null;

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

         return token;
      }

      static void SaveLastToken(SPChangeToken token)
      {
         using (FileStream fs = File.Create(DATA_FILE_PATH))
         {
            // Serialize the token.
            BinaryWriter bw = new BinaryWriter(fs);
            string s = token.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 = "ChangeToken.dat"

   Sub Main()
      Using site As SPSite = New SPSite("https://localhost")

         Dim query As New SPChangeQuery(True, True)
         query.ChangeTokenStart = GetStartingToken()

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

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

            ' This is the starting point for next batch.
            query.ChangeTokenStart = changes.LastChangeToken

            ' If this is the last batch, exit.
            If changes.Count < query.FetchLimit Then
               Exit While
            End If

         End While

         ' Serialize the last token as a starting point for the next run.
         SaveLastToken(query.ChangeTokenStart)
      End Using

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

   End Sub

   Function GetStartingToken() As SPChangeToken
      ' Passing a null token to GetChanges fetches 
      ' changes from the start of the log.
      Dim token As SPChangeToken = Nothing

      ' If we have a token from the last run, use it.
      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 str As String = br.ReadString()
               ' Construct a change token from serialized string.
               token = New SPChangeToken(str)
            Catch e As EndOfStreamException
               ' No serialized string, so do nothing.
            Finally
               br.Close()
            End Try
         End Using
      End If

      Return token
   End Function

   Sub SaveLastToken(ByRef token As SPChangeToken)
      Using fs As FileStream = File.Create(DATA_FILE_PATH)
         ' Serialize the token.
         Dim bw As BinaryWriter = New BinaryWriter(fs)
         Dim s As String = token.ToString()
         bw.Write(s)
         ' Flush and close.
         bw.Flush()
         bw.Close()
      End Using
   End Sub

End Module

請參閱

參照

SPChangeToken class

SPChangeToken members

SPChangeToken overload

Microsoft.SharePoint namespace

SPChangeToken.ToString()