SPWeb.GetChanges 方法 (SPChangeQuery)
Gets changes from the change log filtered by the specified query.
命名空间: Microsoft.SharePoint
程序集: Microsoft.SharePoint(位于 Microsoft.SharePoint.dll 中)
语法
声明
Public Function GetChanges ( _
query As SPChangeQuery _
) As SPChangeCollection
用法
Dim instance As SPWeb
Dim query As SPChangeQuery
Dim returnValue As SPChangeCollection
returnValue = instance.GetChanges(query)
public SPChangeCollection GetChanges(
SPChangeQuery query
)
参数
query
类型:Microsoft.SharePoint.SPChangeQueryThe query that is performed against the change log.
返回值
类型:Microsoft.SharePoint.SPChangeCollection
The changes that have occurred on the website based on the specified query. Specify the maximum number of changes to return by setting the FetchLimit property of the query parameter.
备注
Use this method to get changes to particular objects, rather than all objects, or only for selected actions on particular objects. For more information, see the the SPChangeQuery class.
备注
By default, the change log retains data for 60 days. To change the default retention period, set the ChangeLogRetentionPeriod property.
示例
The following example is a console application that creates a text file with information about list items that have been added, deleted, or updated. The application calls the GetChanges method in a loop, fetching changes in batches until all changes have been retrieved.
using System;
using System.IO;
using Microsoft.SharePoint;
namespace Test
{
class ConsoleApp
{
static void Main(string[] args)
{
using (SPSite siteCollection = new SPSite("https://localhost"))
{
using (SPWeb webSite = siteCollection.RootWeb)
{
// Construct a query
SPChangeQuery query = new SPChangeQuery(false, // limit object types
false); // limit change types
// object type
query.Item = true;
// change types
query.Add = true;
query.Delete = true;
query.Update = true;
SPTimeZone timeZone = webSite.RegionalSettings.TimeZone;
long total = 0;
string fileName = "ItemChanges.txt";
StreamWriter writer = File.AppendText(fileName);
while (true)
{
SPChangeCollection changes = webSite.GetChanges(query);
total += changes.Count;
foreach (SPChangeItem change in changes)
{
// Get the list title
string listTitle = String.Empty;
string itemName = String.Empty;
SPList list = null;
try
{
list = webSite.Lists[change.ListId];
listTitle = list.Title;
}
catch (SPException)
{
listTitle = "Unknown";
}
// Get the item title
if (list != null)
{
SPListItem item = null;
try
{
item = list.GetItemByUniqueId(change.UniqueId);
itemName = item.Name;
}
catch (ArgumentException)
{
itemName = "Unknown";
}
}
// Write to the log
writer.WriteLine("\r\nDate: {0}",
timeZone.UTCToLocalTime(change.Time).ToString());
writer.WriteLine("Change: {0} item", change.ChangeType);
writer.WriteLine("List: {0}", listTitle);
writer.WriteLine("Item: {0}", itemName);
}
// Break out of loop if we have the last batch
if (changes.Count < query.FetchLimit)
break;
// Otherwise, go get another batch
query.ChangeTokenStart = changes.LastChangeToken;
}
writer.WriteLine("\r\nTotal changes = {0:#,#}", total);
writer.Flush();
writer.Close();
Console.WriteLine("{0} changes written to {1}", total, fileName);
}
}
Console.Write("\nPress ENTER to continue...");
Console.ReadLine();
}
}
}
Imports System
Imports System.IO
Imports Microsoft.SharePoint
Module ConsoleApp
Sub Main()
Using siteCollection As SPSite = New SPSite("https://localhost")
Using webSite As SPWeb = siteCollection.RootWeb
' Construct a query
Dim query As New SPChangeQuery(False, False)
' object type
query.Item = True
' change types
query.Add = True
query.Delete = True
query.Update = True
Dim timeZone As SPTimeZone = webSite.RegionalSettings.TimeZone
Dim total As Long = 0
Dim fileName As String = "ItemChanges.txt"
Dim writer As StreamWriter = File.AppendText(fileName)
While True
Dim changes As SPChangeCollection = webSite.GetChanges(query)
total += changes.Count
For Each change As SPChangeItem In changes
' Get the list title
Dim listTitle As String = String.Empty
Dim itemName As String = String.Empty
Dim list As SPList = Nothing
Try
list = webSite.Lists(change.ListId)
listTitle = list.Title
Catch ex As SPException
listTitle = "Unknown"
End Try
' Get the item title
If list IsNot Nothing Then
Dim item As SPListItem = Nothing
Try
item = list.GetItemByUniqueId(change.UniqueId)
itemName = item.Name
Catch ex As ArgumentException
itemName = "Unknown"
End Try
End If
' Write to the log
writer.WriteLine(vbCrLf + "Date: {0}", _
timeZone.UTCToLocalTime(change.Time).ToString())
writer.WriteLine("Change: {0} item", change.ChangeType)
writer.WriteLine("List: {0}", listTitle)
writer.WriteLine("Item: {0}", itemName)
Next change
' Break out of the loop when we fetch the last batch of changes
If changes.Count < query.FetchLimit Then
Exit While
End If
' Go get another batch of changes starting where we left off
query.ChangeTokenStart = changes.LastChangeToken
End While
writer.WriteLine(vbCrLf + "Total changes = {0:#,#}", total)
writer.Flush()
writer.Close()
Console.WriteLine("{0} changes written to {1}", total, fileName)
End Using
End Using
Console.Write(vbCrLf + "Press ENTER to continue...")
Console.ReadLine()
End Sub
End Module