Partilhar via


How to: Filter the Change Log by Object Type

Applies to: SharePoint Foundation 2010

This example is a console application that queries the change log for all changes to SPField and SPContentType objects within the scope of a site collection.

Note that in order to obtain the GUID for the changed object, the example code first casts each SPChange object to a specific subclass, either SPChangeField or SPChangeContentType, before accessing the Id property.

Example

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

See Also

Tasks

How to: Filter the Change Log by Change Type

Concepts

Querying the Change Log for Specific Changes