Share via


Procédure : Filtrer le journal des modifications par type d'objet

Dernière modification : mardi 11 août 2009

S’applique à : SharePoint Foundation 2010

Cet exemple est une application console qui interroge le journal des modifications pour connaître toutes les modifications apportées aux objets SPField et SPContentType dans l'étendue d'une collection de sites.

Notez que pour obtenir le GUID de l'objet modifié, l'exemple de code affecte chaque objet SPChange à une sous-classe spécifique, soit SPChangeField soit SPChangeContentType, avant d'accéder à la propriété Id.

Exemple

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

Voir aussi

Tâches

Procédure : filtrer le journal des modifications par type de modification

Concepts

Recherche modifications spécifiques dans le journal des modifications