Partager via


SPContentDatabase.GetChanges - Méthode (SPChangeQuery)

Retourne une collection des modifications du journal des modifications qui ont été filtrés par la requête spécifiée.

Espace de noms :  Microsoft.SharePoint.Administration
Assembly :  Microsoft.SharePoint (dans Microsoft.SharePoint.dll)

Syntaxe

'Déclaration
Public Function GetChanges ( _
    query As SPChangeQuery _
) As SPChangeCollection
'Utilisation
Dim instance As SPContentDatabase
Dim query As SPChangeQuery
Dim returnValue As SPChangeCollection

returnValue = instance.GetChanges(query)
public SPChangeCollection GetChanges(
    SPChangeQuery query
)

Paramètres

Valeur renvoyée

Type : Microsoft.SharePoint.SPChangeCollection
Collection d'objets SPChange qui représentent les modifications. Vous pouvez ajuster la taille maximale de la collection qui est retournée en définissant la propriété FetchLimit de l'objet SPChangeQuery passé dans le paramètre de requête.

Remarques

Utilisez cette méthode pour filtrer les modifications lorsque vous êtes intéressé uniquement par les modifications apportées à des objets particuliers, plutôt que tous les objets, ou lorsque vous êtes intéressé uniquement par les actions sélectionnées sur des objets particuliers. Pour plus d'informations, consultez la documentation pour la classe SPChangeQuery .

Notes

Par défaut, le journal des modifications conserve les données pendant 60 jours. Vous pouvez configurer la période de rétention en définissant la propriété ChangeLogRetentionPeriod .

Exemples

L'exemple suivant est une application console qui interroge la base de données de contenu pour toutes les modifications apportées aux utilisateurs et aux groupes et imprime des informations sur chaque modification apportée à la console. Notez que l'application appelle la méthode GetChanges dans une boucle, l'extraction des modifications par lots jusqu'à ce que toutes les modifications ont été récupérées.

using System;
using Microsoft.SharePoint;

namespace Test
{
   class ConsoleApp
   {
      static void Main(string[] args)
      {
         using (SPSite siteCollection = new SPSite("https://localhost"))
         {
            using (SPWeb rootSite = siteCollection.RootWeb)
            {
               // Construct a query.
               SPChangeQuery query = new SPChangeQuery(false,  // limit object types
                                                       false); // limit change types
               // object types 
               query.User = true;
               query.Group = true;

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

               // Get the site collection users.
               SPUserCollection users = rootSite.AllUsers;

               // Get the site collection groups.
               SPGroupCollection groups = rootSite.Groups;

               SPTimeZone timeZone = rootSite.RegionalSettings.TimeZone
               long total = 0;
               while (true)
               {
                  SPChangeCollection changes = 
                     siteCollection.ContentDatabase.GetChanges(query);

                  total += changes.Count;

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

                     // Get url of the site where the change took place.
                     foreach (SPSite site in siteCollection.ContentDatabase.Sites)
                     {
                        if (site.ID == change.SiteId)
                        {
                           Console.WriteLine("Site Url: {0}", site.Url);
                           break;
                        }
                     }

                     // Get nature of change.
                     Console.WriteLine("Type of change: {0}", change.ChangeType);

                     // Get information about a user change.
                     if (change is SPChangeUser)
                     {
                        SPChangeUser userChange = (SPChangeUser)change;
                        if (userChange.Activate)
                           Console.WriteLine("This change activated a user.");
                        if (userChange.IsSiteAdminChange)
                           Console.WriteLine("This change made the user a site admin.");

                        // Try to get the user login name.
                        try
                        {
                           SPUser user = users.GetByID(userChange.Id); // Throws an exception if not found
                           Console.WriteLine("Login name: {0}", user.LoginName);

                           // Get information about the user.
                           if (user.IsDomainGroup)
                              Console.WriteLine("This user is a domain group.");
                           if (change.ChangeType == SPChangeType.Update)
                           {
                              Console.Write("Member of:");
                              foreach (SPGroup group in user.Groups)
                                 Console.Write(" {0};", group.Name);
                              Console.WriteLine();
                           }
                        }
                        catch (SPException)
                        {
                           Console.WriteLine("Login name: unknown");
                        }
                     }

                     // Get information about a group change.
                     if (change is SPChangeGroup)
                     {
                        SPChangeGroup groupChange = (SPChangeGroup)change;
                        // Try to get the group name.
                        try
                        {
                           SPGroup group = groups.GetByID(groupChange.Id); // Throws an exception if not found
                           Console.WriteLine("Group name: {0}", group.Name);
                           Console.WriteLine("Number of members: {0}", group.Users.Count);
                        }
                        catch (SPException)
                        {
                           Console.WriteLine("Group name: unknown");
                        }
                     }
                  }

                  // Break out of loop if we have the last batch.
                  if (changes.Count < query.FetchLimit)
                     break;
                  // Otherwise, go get another batch.
                  query.ChangeTokenStart = changes.LastChangeToken;
               }

               Console.WriteLine("\nTotal changes = {0:#,#}", total);
            }
         }
         Console.Write("\nPress ENTER to continue...");
         Console.ReadLine();
      }
   }
}
Imports System
Imports Microsoft.SharePoint

Module ConsoleApp
   Sub Main()
      Using siteCollection As SPSite = New SPSite("https://localhost")
         Using rootSite As SPWeb = siteCollection.RootWeb

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

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

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

            ' Get the site collection users.
            Dim users As SPUserCollection = rootSite.AllUsers

            ' Get the site collection groups.
            Dim groups As SPGroupCollection = rootSite.Groups

            Dim timeZone As SPTimeZone = rootSite.RegionalSettings.TimeZone
            Dim total As Long = 0
            While True

               Dim changes As SPChangeCollection = _
                   siteCollection.ContentDatabase.GetChanges(query)

               total += changes.Count

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

                  ' Print the url of the site where the change took place.
                  For Each site As SPSite In siteCollection.ContentDatabase.Sites
                     If site.ID = change.SiteId Then
                        Console.WriteLine("Site Url: {0}", site.Url)
                        Exit For
                     End If
                  Next site

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

                  ' Get information about a user change.
                  If TypeOf change Is SPChangeUser Then
                     Dim userChange As SPChangeUser = CType(change, SPChangeUser)
                     If userChange.Activate Then
                        Console.WriteLine("This change activated a user.")
                     End If
                     If userChange.IsSiteAdminChange Then
                        Console.WriteLine("This change made the user a site admin.")
                     End If
                     ' Try to get the user login name.
                     Try
                        Dim user As SPUser = users.GetByID(userChange.Id) ' Throws an exception if not found
                        Console.WriteLine("Login name: {0}", user.LoginName)

                        ' Get information about the user.
                        If user.IsDomainGroup Then
                           Console.WriteLine("This user is a domain group.")
                        End If
                        If change.ChangeType = SPChangeType.Update Then
                           Console.Write("Member of:")
                           For Each group As SPGroup In user.Groups
                              Console.Write(" {0};", group.Name)
                           Next group
                           Console.WriteLine()
                        End If
                     Catch ex As SPException
                        Console.WriteLine("Login name: unknown")
                     End Try
                  End If

                  ' Get information about a group change.
                  If TypeOf change Is SPChangeGroup Then
                     Dim groupChange As SPChangeGroup = CType(change, SPChangeGroup)
                     ' Try to get the group name.
                     Try
                        Dim group As SPGroup = groups.GetByID(groupChange.Id) ' Throws an exception if not found
                        Console.WriteLine("Group name: {0}", group.Name)
                        Console.WriteLine("Number of members: {0}", group.Users.Count)
                     Catch ex As SPException
                        Console.WriteLine("Group name: unknown")
                     End Try
                  End If

               Next change

               ' Break out of the loop when we fetch the last batch of changes.
               If changes.Count < query.FetchLimit Then
                  Exit While
               End If

               ' Go get another batch of changes starting where we left off.
               query.ChangeTokenStart = changes.LastChangeToken

            End While

            Console.WriteLine(vbCrLf + "Total changes = {0:#,#}", total)

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

Voir aussi

Référence

SPContentDatabase classe

SPContentDatabase - Membres

GetChanges - Surcharge

Microsoft.SharePoint.Administration - Espace de noms

Autres ressources

Using the Change Log