SPChangeTokenCollection.ToString method

Gets the serialized string representation of the change token collection.

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

Syntax

'Declaration
Public Overrides Function ToString As String
'Usage
Dim instance As SPChangeTokenCollection
Dim returnValue As String

returnValue = instance.ToString()
public override string ToString()

Return value

Type: System.String
A string that contains the serialized string representation of the collection.

Remarks

You can use this method to serialize a change token collection before persisting it to permanent storage. To reconstruct the collection, pass the serialized string representation of the collection to the overload of the SPChangeTokenCollection constructor that accepts a string.

Examples

The following example consists of two routines from a larger application. The first is a procedure that writes a change token collection to a file on disk. The second is a function that accepts a file name as an argument, opens the file, reads the first serialized string variable that it finds, and uses that to create a change token collection, the function’s return value. Note that if the file is not found or does not contain a correctly formatted string variable, the function returns an empty collection.

Sub SaveTokens(ByRef tokens As SPChangeTokenCollection, ByVal filePath As String)
  Using fs As FileStream = File.Create(filePath)
     ' 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

Function GetTokens(ByVal filePath As String) As SPChangeTokenCollection

  Dim tokens As SPChangeTokenCollection = New SPChangeTokenCollection()

  ' If we have a persisted collection, use it
  If File.Exists(filePath) Then
     Using fs As FileStream = File.OpenRead(filePath)
        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 
           ' No serialized string, or an incorrectly formatted string.
           ' Do nothing. We'll return an empty collection.
        Finally
           br.Close()
        End Try
     End Using
  End If
  Return tokens
End Function
static void SaveTokens(SPChangeTokenCollection tokens, string filePath)
{
   using (FileStream fs = File.Create(filePath))
   {
      // Serialize the tokens
      BinaryWriter bw = new BinaryWriter(fs);
      string s = tokens.ToString();
      bw.Write(s);

      // flush and close
      bw.Flush();
      bw.Close();
   }
}

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

   // If we have a persisted collection, use it
   if (File.Exists(filePath))
   {
      using (FileStream fs = File.OpenRead(filePath))
      {
         BinaryReader br = new BinaryReader(fs);
         try
         {
            string s = br.ReadString();
            // Construct a change token from string
            tokens = new SPChangeTokenCollection(s);
         }
         catch 
         {
            // No serialized string, or an incorrectly formatted string.
            // Do nothing. We'll return an empty collection.
         }
         finally
         {
            br.Close();
         }
      }
   }

   return tokens;
}

See also

Reference

SPChangeTokenCollection class

SPChangeTokenCollection members

Microsoft.SharePoint namespace

ToString()