Compartir a través de


Procedimiento para filtrar el registro de cambios por tipo de cambio

Última modificación: miércoles, 29 de julio de 2009

Hace referencia a: SharePoint Foundation 2010

En este ejemplo se muestra una aplicación de consola que consulta el registro de cambios en el ámbito de la base de datos de contenido. Se consulta por cambios en los objetos SPUser y SPGroup que impliquen la adición, actualización o eliminación de uno de estos tipos de objetos y cambios que modifiquen la pertenencia a grupos.

Tenga en cuenta que en el ejemplo se intenta reflejar el hecho de que la seguridad del registro de cambios se administra en forma separada de la seguridad de los objetos en el sistema. Al compilar y ejecutar el ejemplo, los privilegios podrían permitir que lea el registro de cambios y descubra qué tipo de objetos cambiaron y qué tipo de cambios se realizaron en ellos, pero es posible que no tenga permiso para obtener acceso a los propios objetos. Como consecuencia, el código podría imprimir información que dice que, en una cierta fecha, se agregó un objeto SPUser a una colección de sitios y el GUID para el objeto que se agregó, pero igual emitiría una UnauthorizedAccessException cuando intente recuperar el objeto. No es lo mismo tener permiso para obtener acceso al registro de cambios que tener permiso para obtener acceso a los objetos cambiados.

Ejemplo

using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace Test
{
    class Program
    {
        static SPUserCollection m_users;
        static SPGroupCollection m_groups;

        static void Main(string[] args)
        {
            using (SPSite currentSite = new SPSite("https://localhost"))
            {
                SPTimeZone tz = currentSite.RootWeb.RegionalSettings.TimeZone;
                SPContentDatabase db = currentSite.ContentDatabase;
                m_users = currentSite.RootWeb.AllUsers;
                m_groups = currentSite.RootWeb.Groups;

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

                // Specify change types. 
                query.Add = true;
                query.Delete = true;
                query.Update = true;
                query.GroupMembershipAdd = true;
                query.GroupMembershipDelete = true;

                long total = 0;
                while (true)
                {
                    SPChangeCollection changes = db.GetChanges(query);

                    total += changes.Count;

                    foreach (SPChange change in changes)
                    {
                        // Print the date of the change.
                        Console.WriteLine("\nDate: {0}",
                                          tz.UTCToLocalTime(change.Time).ToString());

                        // Get the url, user collection, and group collection
                        // of the site where the change took place.
                        foreach (SPSite site in db.Sites)
                        {
                            if (site.ID == change.SiteId)
                            {
                                Console.WriteLine("Site Url: {0}", site.Url);
                                try
                                {
                                    m_users = site.RootWeb.AllUsers;
                                    m_groups = site.RootWeb.Groups;
                                }
                                catch (UnauthorizedAccessException)
                                {
                                    // Do nothing. The failure is handled elsewhere in the code.
                                }
                                finally
                                {
                                    site.Dispose();
                                }
                                break;
                            }
                            site.Dispose();
                        }

                        // Print the nature of the change.
                        Console.WriteLine("Type of object: {0}", change.GetType().ToString());
                        Console.WriteLine("Type of change: {0}", change.ChangeType.ToString());

                        // Get information about a user change.
                        if (change is SPChangeUser)
                        {
                            SPChangeUser userChange = (SPChangeUser)change;
                            // Print the user name.
                            string userName = GetPrincipalName(userChange.Id, true);
                            Console.WriteLine("User name: {0}", userName);
                        }

                        // Get information about a group change.
                        if (change is SPChangeGroup)
                        {
                            SPChangeGroup groupChange = (SPChangeGroup)change;
                            // Print the group name.
                            string groupName = GetPrincipalName(groupChange.Id, false);
                            Console.WriteLine("Group name: {0}", groupName);

                            if (groupChange.ChangeType == SPChangeType.MemberAdd ||
                                groupChange.ChangeType == SPChangeType.MemberDelete)
                            {
                                string userName = GetPrincipalName(groupChange.UserId, true);
                                Console.WriteLine("User name: {0}", userName);
                            }
                        }
                    }
                    if (changes.Count < query.FetchLimit)
                        break;
                    query.ChangeTokenStart = changes.LastChangeToken;
                }
                Console.WriteLine("\nTotal changes = {0:#,#}", total);
            }
            Console.Write("\nPress ENTER to continue...");
            Console.Read();
        }

        static string GetPrincipalName(int id, bool isUser)
        { 
            string name = string.Empty;
            try
            {
                if (isUser)
                {
                    SPUser user = m_users.GetByID(id);
                    name = user.LoginName;
                }
                else
                {
                    SPGroup group = m_groups.GetByID(id);
                    name = group.Name;
                }
            }
            catch (UnauthorizedAccessException)
            {
                name = "unknown (access not authorized)";
            }
            catch (SPException)
            {
                name = "unknown (not found)";
            }
            return name;
        }
    }
}
Imports System
Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.Administration

Module ConsoleApp

   Dim m_users As SPUserCollection
   Dim m_groups As SPGroupCollection

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

         Dim tz As SPTimeZone = currentSite.RootWeb.RegionalSettings.TimeZone
         Dim db As SPContentDatabase = currentSite.ContentDatabase
         m_users = currentSite.RootWeb.Users
         m_groups = currentSite.RootWeb.Groups

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

         ' Specify object types.
         query.User = True
         query.Group = True

         ' Specify change types. 
         query.Add = True
         query.Delete = True
         query.Update = True
         query.GroupMembershipAdd = True
         query.GroupMembershipDelete = True

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

            Dim change As SPChange
            For Each change In changes
               ' Print the date of the change.
               Console.WriteLine(vbCrLf + "Date: {0}", _
                                    tz.UTCToLocalTime(change.Time).ToString())

               ' Get the url, user collection, and group collection
               ' of the site where the change took place.
               Dim site As SPSite
               For Each site In db.Sites
                  If site.ID = change.SiteId Then
                     Console.WriteLine("Site Url: {0}", site.Url)

                     Try
                        m_users = site.RootWeb.AllUsers
                        m_groups = site.RootWeb.Groups
                     Catch
                        ' Do nothing. The failure is handled elsewhere in the code.
                     Finally
                        site.Dispose()
                     End Try

                     Exit For
                  End If
                  site.Dispose()
               Next

               ' Print the nature of the change.
               Console.WriteLine("Type of object: {0}", change.GetType().ToString())
               Console.WriteLine("Type of change: {0}", change.ChangeType.ToString())

               ' Get information about a user change.
               If TypeOf change Is SPChangeUser Then
                  Dim userChange As SPChangeUser = CType(change, SPChangeUser)
                  ' Print the user name.
                  Dim userName As String = GetPrincipalName(userChange.Id, True)
                  Console.WriteLine("User name: {0}", userName)
               End If

               ' Get information about a group change.
               If TypeOf change Is SPChangeGroup Then
                  Dim groupChange As SPChangeGroup = CType(change, SPChangeGroup)
                  ' Print the group name.
                  Dim groupName As String = GetPrincipalName(groupChange.Id, False)
                  Console.WriteLine("Group name: {0}", groupName)

                  If (groupChange.ChangeType = SPChangeType.MemberAdd) Or _
                     (groupChange.ChangeType = SPChangeType.MemberDelete) Then
                     Dim userName As String = GetPrincipalName(groupChange.UserId, True)
                     Console.WriteLine("User name: {0}", userName)
                  End If
               End If

            Next

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

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

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

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

   Function GetPrincipalName(ByVal id As Integer, ByVal isUser As Boolean) As String
      Dim name As String = String.Empty
      Try
         If isUser Then
            Dim user As SPUser = m_users.GetByID(id)
            name = user.LoginName
         Else
            Dim group As SPGroup = m_groups.GetByID(id)
            name = group.Name
         End If
      Catch secex As UnauthorizedAccessException
         name = "unknown (access not authorized)"
      Catch spex As SPException
         name = "unknown (not found)"
      End Try
      Return name
   End Function

End Module

Vea también

Tareas

Procedimiento para fitrar el registro de cambios por tipo de objeto

Conceptos

Consulta de cambios específicos en el registro de cambios