Export (0) Print
Expand All

How to: Filter the Change Log by Object Type

SharePoint 2010

Last modified: August 11, 2009

Applies to: SharePoint Foundation 2010

This example is a console application that queries the change log for all changes to SPField and SPContentType objects within the scope of a site collection.

Note that in order to obtain the GUID for the changed object, the example code first casts each SPChange object to a specific subclass, either SPChangeField or SPChangeContentType, before accessing the Id property.

using System;
using Microsoft.SharePoint;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("http://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();
        }
    }
}
Show:
© 2015 Microsoft