SharePoint Object Model "Hidden Gems"

Posted in SharePoint Products and Technologies at Saturday, October 27, 2007 3:57 PM Eastern Daylight Time

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:

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 + "<BR>");
            }
            catch
            {
                Response.Write("The object to which item " + changedItem.UniqueId + " belongs does not exist.<BR>");
            }
            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.spchangesecuritypolicy.aspx

 

SPChangeUser

Represents a change to a user

http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spchangeuser.aspx

View Lamont Harrington's profile on LinkedIn

Microsoft Employee


Calendar

<October 2007>
SunMonTueWedThuFriSat
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

On this page...

Tags


Archives