Partilhar via


SPChangeCollection.Count Property

Gets the number of changes in the collection.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online

Syntax

'Declaration
Public Overrides ReadOnly Property Count As Integer
    Get
'Usage
Dim instance As SPChangeCollection
Dim value As Integer

value = instance.Count
public override int Count { get; }

Property Value

Type: System.Int32
A 32-bit integer that indicates the number of changes.

Implements

ICollection.Count

Examples

The following example is a console application that queries the change log for role assignments that have been added to a Web site. The application calls the SPWeb.GetChanges method in a loop, retrieving successive change collections until the end of the change log is reached. During this process, the application keeps a running total by adding the value of the current collection's Count property to a variable. When the last batch of changes has been retrieved, the application prints the total to the console.

using System;
using Microsoft.SharePoint;

namespace Test
{
   class ConsoleApp
   {
      static void Main(string[] args)
      {
         using (SPSite siteCollection = new SPSite("https://localhost"))
         {
            using (SPWeb webSite = siteCollection.OpenWeb())
            {
               // Get a list.
               SPList list = webSite.Lists[0];

               // Construct a query.
               SPChangeQuery query = new SPChangeQuery(false,  // limit object types
                                                       false); // limit change types

               // Specify the object type. 
               query.Item = true;

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

               SPTimeZone timeZone = webSite.RegionalSettings.TimeZone;
               int total = 0;

               // Loop until we reach the end of the log.
               while (true)
               {
                  SPChangeCollection changes = list.GetChanges(query);

                  total += changes.Count;

                  // Print info about each change to the console.
                  foreach (SPChangeItem change in changes)
                  {
                     // Get the item name.
                     string itemName = String.Empty;
                     SPListItem item = null;
                     try
                     {
                        item = list.GetItemByUniqueId(change.UniqueId);
                        itemName = item.Name;
                     }
                     catch (ArgumentException)
                     {
                        itemName = "Unknown";
                     }

                     Console.WriteLine("\nDate: {0}",
                         timeZone.UTCToLocalTime(change.Time).ToString());
                     Console.WriteLine("Change: {0}", change.ChangeType);
                     Console.WriteLine("Item: {0}", itemName);

                  }

                  // 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 of {0} changes to {1} list", total, list.Title);
            }
         }
         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 webSite As SPWeb = siteCollection.RootWeb

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

            ' object type
            query.Web = True

            ' change type 
            query.RoleAssignmentAdd = True

            Dim total As Long = 0
            While True

               Dim changes As SPChangeCollection = webSite.GetChanges(query)

               total += changes.Count

               ' 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("{0} role assignments added", total)

         End Using
      End Using

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

   End Sub
End Module

See Also

Reference

SPChangeCollection Class

SPChangeCollection Members

Microsoft.SharePoint Namespace

Other Resources

Using the Change Log