SharePoint Object Model “Hidden Gems”

I was recently presented a scenario by a gentleman who wanted more control over a MOSS 2007 site, list, and item settings.  He wanted to essentially “track” changes that were made to these artifacts within a given site collection.  In addition, he wanted to know the type of change that occured.

SPListEventReceiver or SPWebEventReceiver you might suggest?  Not quite the solution here.  One of the “hidden gems” within the SharePoint object model is the SPChange class.  This class represents a change that has been made to objects or metadata within an item, list, Web site, or site collection scope, or a security policy change at the Web application scope that has been recorded in the Microsoft Windows SharePoint Services change log.  An example of this functionality is cited in the code example below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
SPSite siteCollection = SPContext.Current.Site;SPWeb site = siteCollection.AllWebs["Site"];
SPListCollection lists = site.Lists;
SPRegionalSettings regionSettings = site.RegionalSettings;
SPTimeZone timeZone = regionSettings.TimeZone;
SPChangeQuery query = new SPChangeQuery(true, true);
SPChangeCollection changes = site.GetChanges(query);
 
foreach (SPChange changedObject in changes)
{
  switch (changedObject.GetType().ToString())
  {
    case "Microsoft.SharePoint.SPChangeItem":
      SPChangeItem changedItem = (SPChangeItem)changedObject;
      try
      {
        Response.Write(lists[changedItem.ListId].Title + " == " + lists[changedItem.ListId].GetItemByUniqueId(changedItem.UniqueId).Name + " == "
          + timeZone.UTCToLocalTime(changedItem.Time) + " == " + changedItem.ChangeType + "
");
      }
      catch
      {
        Response.Write("The object to which item " + changedItem.UniqueId + " belongs does not exist.
");
      }
      break;
  }
}

The code snippet above allows you to display the names of lists in which items in a site has changed.  The SPChange object provides a powerful mechanism for retrieving this type of information in MOSS.  Note the use of the GetChanges() method that is being called on the SPSite object in the code above.  The GetChanges() method of the SPList, SPWeb, SPSite, or SPContentDatabase object returns the collection of changes that have occurred within the given scope.

In addition to SPChange, there are quite a few other classes that have similar functionality and represent various types of changes that occur during the lifecycle of a SharePoint site, list or item.  I’ve cited a few of them below:

SPChangeGroup
Represents a change to a group
http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spchangegroup.aspx 

SPChangeSecurityPolicy
Represents a change to a security policy
http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spchangegroup.aspx 

SPChangeUser
Represents a change to a user
http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spchangegroup.aspx 

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by WP-CopyRightPro