初始化SPChangeToken类中,根据其序列化的表单的新实例。
命名空间: Microsoft.SharePoint
程序集: Microsoft.SharePoint(位于 Microsoft.SharePoint.dll 中)
语法
声明
Public Sub New ( _
strChangeToken As String _
)
用法
Dim strChangeToken As String
Dim instance As New SPChangeToken(strChangeToken)
public SPChangeToken(
string strChangeToken
)
参数
strChangeToken
类型:System.String更改令牌序列化窗体。
异常
| 异常 | 条件 |
|---|---|
| InvalidOperationException | strChangeToken不包含正确的令牌。 AD 访问控制 指定strChangeToken的版本不是 1。 |
备注
此构造函数的重载可用于重建已序列化,然后存储在磁盘上的更改令牌。有关序列化SPChangeToken对象的信息,请参阅ToString方法。
序列化的令牌包含按以下顺序 (;),用分号隔开的五个字段:
与int的字符串表示形式的版本号。
范围,由Scope属性表示。
由ScopeId属性表示范围 ID。
日期和时间DateTime对象的字符串表示形式的更改。
将更改数字显示为long的字符串表示形式。
示例
下面的示例是一个控制台应用程序,用于查询有关内容数据库范围更改的更改日志。
首次运行的应用程序,日志将查询从日志的开头开始的所有更改。由第一次将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