SPChangeQuery-Klasse

Definiert eine Abfrage, die für das Änderungsprotokoll in Microsoft SharePoint Foundationdurchgeführt wird.

Vererbungshierarchie

System.Object
  Microsoft.SharePoint.SPChangeQuery

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

'Declaration
Public NotInheritable Class SPChangeQuery
'Usage
Dim instance As SPChangeQuery
public sealed class SPChangeQuery

Hinweise

Verwenden Sie ein SPChangeQuery -Objekt zum Definieren einer Abfrage, die Sie als Argument an eine Methode GetChanges der Klasse SPList, SPWeb, SPSiteoder SPContentDatabase übergeben können.

Die Eigenschaften der SPChangeQuery -Klasse können verwendet werden, um Filter für die Abfrage anzugeben. Es gibt zwei Typen von Eigenschaften: gelten in den Typ des Objekts, das geändert wurde und gelten in den Typ der Änderung, die aufgetreten sind. Verwenden Sie diese Eigenschaften in Verbindung mit den SPChangeQuery -Konstruktor eine Abfrage definieren, die bestimmte Daten aus dem Änderungsprotokoll zurück.

Beispiele

Das folgende Beispiel ist eine Konsolenanwendung, die die Benutzernamen der Benutzer druckt die Gruppen innerhalb einer Websitesammlung sowie die Gruppen hinzugefügt wurden, hinzugefügt wurden und das Datum der Änderung.

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, false); 

               // Set a limit on the number of changes returned on a single trip.
               query.FetchLimit = 500;

               // object type 
               query.Group = true;

               // change type
               query.GroupMembershipAdd = true;

               // Get the users and groups for the site collection.
               SPUserCollection users = rootSite.AllUsers;
               SPGroupCollection groups = rootSite.Groups;

               // Convert to local time.
               SPTimeZone timeZone = rootSite.RegionalSettings.TimeZone;

               // total changes
               int total = 0;

               // Loop until we reach the end of the log.
               while (true)
               {
                  SPChangeCollection changes = siteCollection.GetChanges(query);
                  total += changes.Count; // running total

                  foreach (SPChangeGroup change in changes)
                  {
                     // Try to get the group name.
                     string groupName = String.Empty;
                     try
                     {
                        SPGroup group = groups.GetByID(change.Id);
                        groupName = group.Name;
                     }
                     catch (SPException)
                     {
                        groupName = "Unknown";
                     }

                     // Try to get the user name.
                     string loginName = String.Empty;
                     try
                     {
                        SPUser user = users.GetByID(change.UserId);
                        loginName = user.LoginName;
                     }
                     catch (SPException)
                     {
                        loginName = "Unknown";
                     }

                     // Write to the console.
                     Console.WriteLine("\nDate: {0}", 
                          timeZone.UTCToLocalTime(change.Time).ToString());
                     Console.WriteLine("{0} was added to the {1} group.", 
                          loginName, groupName);
                  }

                  // 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)

            ' Set a limit on the number of changes returned on a single trip.
            query.FetchLimit = 500

            ' Select the object type. 
            query.Group = True

            ' Select the change type.
            query.GroupMembershipAdd = True

            ' Get the users and groups for the site collection.
            Dim users As SPUserCollection = rootSite.AllUsers
            Dim groups As SPGroupCollection = rootSite.Groups

            ' Convert to local time.
            Dim timeZone As SPTimeZone = rootSite.RegionalSettings.TimeZone

            ' total changes
            Dim total As Integer = 0

            ' Loop until we reach the end of the log.
            While True
               Dim changes As SPChangeCollection = siteCollection.GetChanges(query)
               total += changes.Count ' running total
               For Each change As SPChangeGroup In changes

                  ' Try to get the group name.
                  Dim groupName As String = String.Empty
                  Try
                     Dim group As SPGroup = groups.GetByID(change.Id)
                     groupName = group.Name
                  Catch ex As SPException
                     groupName = "Unknown"
                  End Try

                  ' Try to get the user name.
                  Dim loginName As String = String.Empty
                  Try
                     Dim user As SPUser = users.GetByID(change.UserId)
                     loginName = user.LoginName
                  Catch ex As SPException
                     loginName = "Unknown"
                  End Try

                  ' Write to the console.
                  Console.WriteLine(vbCrLf + "Date: {0}", _
                                    timeZone.UTCToLocalTime(change.Time).ToString())
                  Console.WriteLine("{0} was added to the {1} group.", _
                                    loginName, groupName)

               Next change

               ' Break out of loop if we have the last batch.
               If changes.Count < query.FetchLimit Then
                  Exit While
               End If
               ' Otherwise, go get another batch.
               query.ChangeTokenStart = changes.LastChangeToken
            End While

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

         End Using
      End Using

      Console.Write(vbCrLf + "Press ENTER to continue...")
      Console.ReadLine()

   End Sub
End Module

Threadsicherheit

Alle öffentlichen static (Shared in Visual Basic) Member dieses Typs sind threadsicher. Die Threadsicherheit von Instanzmembern ist nicht gewährleistet.

Siehe auch

Referenz

SPChangeQuery-Member

Microsoft.SharePoint-Namespace