SPChangeTokenCollection.ToString method

Gets the serialized string representation of the change token collection.

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

public override string ToString()

Return value

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

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.

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.

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;
}
Show: