如何:按对象类型筛选更改日志

上次修改时间: 2009年8月11日

适用范围: SharePoint Foundation 2010

本示例是一个控制台应用程序,可以在网站集范围内查询更改日志,以获取对 SPFieldSPContentType 对象所做的所有更改。

请注意,为了获取已更改对象的 GUID,该示例代码先将各个 SPChange 对象强制转换为特定子类(SPChangeFieldSPChangeContentType),然后再访问 Id 属性。

示例

using System;
using Microsoft.SharePoint;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("https://localhost"))
            {
                SPTimeZone tz = site.RootWeb.RegionalSettings.TimeZone;

                // Construct a query.
                SPChangeQuery query = new SPChangeQuery(false,  // Specify object types
                                                        true    // All change types
                                                        );

                // Specify object types.
                query.Field = true;
                query.ContentType = true;

                // Get the fields and content types in this site.
                SPFieldCollection fields = site.RootWeb.Fields;
                SPContentTypeCollection contentTypes = site.RootWeb.AvailableContentTypes;

                long total = 0;
                while (true)
                {
                    SPChangeCollection changes = site.GetChanges(query);
                    total += changes.Count;

                    foreach (SPChange change in changes)
                    {
                        string name = String.Empty;
                        string id = Guid.Empty.ToString();

                        if (change is SPChangeField)
                        {
                           SPChangeField changedField = change as SPChangeField;
                           id = changedField.Id.ToString();

                           try
                           {
                              name = fields[changedField.Id].InternalName;
                           }
                           catch (ArgumentException)
                           {
                              name = "deleted field";
                           }
                        }

                        if (change is SPChangeContentType)
                        {
                           SPChangeContentType changedContentType = change as SPChangeContentType;
                           id = changedContentType.Id.ToString();

                           try
                           {
                              name = contentTypes[changedContentType.Id].Name;
                           }
                           catch (NullReferenceException)
                           {
                              name = "deleted content type";
                           }
                        }

                        Console.WriteLine("\nDate: {0}", tz.UTCToLocalTime(change.Time).ToString());
                        Console.WriteLine("Object type: {0}", change.GetType().ToString());
                        Console.WriteLine("Id: {0}", id);
                        Console.WriteLine("Name: {0}", name);
                        Console.WriteLine("Change type: {0}", change.ChangeType);
                    }

                    if (changes.Count < query.FetchLimit)
                        break;

                    query.ChangeTokenStart = changes.LastChangeToken;
                    changes = site.GetChanges(query);
                }
                Console.WriteLine("\nTotal changes: {0}", total);
            }
            Console.Write("\nPress ENTER to continue...");
            Console.Read();
        }
    }
}
Imports System
Imports Microsoft.SharePoint

Module ConsoleApp

   Sub Main()
      Using site As SPSite = New SPSite("https://localhost")

         Dim tz As SPTimeZone = site.RootWeb.RegionalSettings.TimeZone

         ' Construct a query.
         Dim query As SPChangeQuery = New SPChangeQuery(False, True)

         ' Specify object types.
         query.Field = True
         query.ContentType = True

         ' Get the fields and content types in this site.
         Dim fields As SPFieldCollection = site.RootWeb.Fields
         Dim contentTypes As SPContentTypeCollection = site.RootWeb.AvailableContentTypes

         Dim total As Long = 0
         While True
            Dim changes As SPChangeCollection = site.GetChanges(query)
            total += changes.Count

            Dim change As SPChange
            For Each change In changes
               Dim name As String = String.Empty
               Dim id As String = Guid.Empty.ToString()

               If TypeOf change Is SPChangeField Then
                   Dim changedField As SPChangeField =  change as SPChangeField 
                   id = changedField.Id.ToString()
 
                   Try
                      name = fields(changedField.Id).InternalName
                   Catch
                      name = "deleted field"
                   End Try
               End If
 
               If TypeOf change Is SPChangeContentType Then
                   Dim changedContentType As SPChangeContentType =  change as SPChangeContentType 
                   id = changedContentType.Id.ToString()
 
                   Try
                      name = contentTypes(changedContentType.Id).Name
                   Catch
                      name = "deleted content type"
                   End Try
               End If

               Console.WriteLine(vbCrLf + "Date: {0}", tz.UTCToLocalTime(change.Time).ToString())
               Console.WriteLine("Object type: {0}", change.GetType().ToString())
               Console.WriteLine("Id: {0}", id)
               Console.WriteLine("Name: {0}", name)
               Console.WriteLine("Change type: {0}", change.ChangeType)
            Next

            If changes.Count < changes.FetchLimit Then
               Exit While
            End If

            query.ChangeTokenStart = changes.LastChangeToken
            changes = site.GetChanges(query)

         End While
         Console.WriteLine(vbCrLf + "Total changes: {0}", total)
      End Using

      Console.Write(vbCrLf + "Press ENTER to continue...")
      Console.Read()
   End Sub
End Module

请参阅

任务

如何:按更改类型筛选更改日志

概念

查询更改日志以获取特定更改