Share via


SPChangeToken - Constructeur (String)

Initialise une nouvelle instance de la classe SPChangeToken , en fonction de sa forme sérialisée.

Espace de noms :  Microsoft.SharePoint
Assembly :  Microsoft.SharePoint (dans Microsoft.SharePoint.dll)

Syntaxe

'Déclaration
Public Sub New ( _
    strChangeToken As String _
)
'Utilisation
Dim strChangeToken As String

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

Paramètres

  • strChangeToken
    Type : System.String

    La forme sérialisée du jeton de modification.

Exceptions

Exception Condition
InvalidOperationException

Le strChangeToken ne contient pas les jetons corrects.

- ou -

La version spécifiée par la strChangeToken n'est pas 1.

Remarques

Cette surcharge du constructeur est utile pour la reconstruction d'un jeton de modification qui a été sérialisé et puis stocké sur le disque. Pour plus d'informations sur la sérialisation d'un objet SPChangeToken , consultez la méthode ToString .

Le jeton sérialisé comporte cinq champs séparés par des points-virgules (;), dans l'ordre suivant :

  • Le numéro de version comme la représentation sous forme de chaîne d'un int.

  • L'étendue, qui est représenté par la propriété Scope .

  • L'ID d'étendue, qui est représentée par la propriété ScopeId .

  • La date et l'heure de la modification de la représentation sous forme de chaîne d'un objet DateTime .

  • Le numéro de modification comme la représentation sous forme de chaîne d'un long.

Exemples

L'exemple suivant est une application console qui interroge le journal des modifications qui ont une délimitation de base de données de contenu.

La première fois que l'application s'exécute, le journal est interrogé pour toutes les modifications à partir du début du journal. Cette requête est exécutée par tout d'abord définir la propriété ChangeTokenStart d'un objet SPChangeQuery à une valeur null et en passant ensuite l'objet SPQuery à la méthode GetChanges . Une fois que toutes les modifications ont été traitées, le programme sérialise le dernier jeton de modification depuis le dernier lot de modifications en appelant la méthode ToString() et stocke le résultat dans un fichier sur disque.

Lors des exécutions suivantes, le programme désérialise le jeton de modification stockée et l'utilise pour définir la propriété ChangeTokenStart de sa requête contre le journal des modifications. Le travail s'effectue dans la fonction GetStartingToken , qui est où le constructeur SPChangeToken est appelé.

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

Voir aussi

Référence

SPChangeToken classe

SPChangeToken - Membres

SPChangeToken - Surcharge

Microsoft.SharePoint - Espace de noms

SPChangeToken.ToString()