Compartir a través de


Procedimiento para fitrar el registro de cambios por tipo de objeto

Última modificación: martes, 11 de agosto de 2009

Hace referencia a: SharePoint Foundation 2010

Este ejemplo es una aplicación de consola que consulta el registro de cambios para conocer todos los cambios en los objetos SPField y SPContentType dentro del ámbito de la colección de sitios.

Tenga en cuenta que, para obtener el GUID del objeto cambiado, en el código de ejemplo se convierte cada objeto SPChange en una subclase específica, SPChangeField o SPChangeContentType, antes de obtener acceso a la propiedad Id.

Ejemplo

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

Vea también

Tareas

Procedimiento para filtrar el registro de cambios por tipo de cambio

Conceptos

Consulta de cambios específicos en el registro de cambios