Compartilhar via


SPChangeToken.ToString Method

Returns the serialized string that represents the change token.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online

Syntax

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

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

Return Value

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

Remarks

You can use this method to serialize a change token before persisting it to permanent storage. To reconstruct the token, pass the serialized string representation to the SPChangeToken(String) constructor.

Examples

The following example consists of two routines, one to serialize a change token and save it on disk, the other to reconstruct a token that has been saved.

Sub SaveChangeToken(ByVal token As SPChangeToken, ByVal fileName As String)
  Using fs As FileStream = File.Create(fileName)

     ' 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

Function GetChangeToken(ByVal fileName As String) As SPChangeToken
  Dim token As SPChangeToken = Nothing

  ' If we have a token, use it.
  If File.Exists(fileName) Then
     Using fs As FileStream = File.OpenRead(fileName)
        Dim br As BinaryReader = New BinaryReader(fs)
        Try
           Dim s As String = br.ReadString()
           ' Construct a change token from a serialized string
           token = New SPChangeToken(s)
        Catch e As EndOfStreamException
           ' No serialized string, so do nothing.
        Finally
           br.Close()
        End Try
     End Using
  End If
  Return token
End Function
static void SaveChangeToken(SPChangeToken token, string fileName)
{
   using (FileStream fs = File.Create(fileName))
   {
      // Serialize the token.
      BinaryWriter bw = new BinaryWriter(fs);
      string s = token.ToString();
      bw.Write(s);

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

static SPChangeToken GetChangeToken(string fileName)
{
   SPChangeToken token = null;

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

   return token;
}

See Also

Reference

SPChangeToken Class

SPChangeToken Members

Microsoft.SharePoint Namespace