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 + "
");
}
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
Related posts:
Nitin,
You’re right, the event receivers are a good option when you want to perform some actionable task (ex: kick off a workflow or execute some custom logic as a result of the event you’re trapping for being fired). You could even write code to log all changes that have occured to a given list.
The problem with this approach is you’ve have to implement event receivers on each list you want to track actions on and have to manually code up the "roll up" logic to log centrally. The snippet above uses SharePoint’s built-in logging to track all of these actions for you without you having to implement custom code to essentially duplicate the functionality.
Think of the Windows Event Log and how Windows essentially logs system messages to it automatically and how difficult it would be to write code that captures *all* Windows events you care about. In this scenario here, he wanted to essentially know what’s going on in his site collections. The code snippet above provides this functionality without too much pain
11:47 pm
But, isnt the event listener/receiver the best option if you want to do some task as a result of an action on the sharepoint object; in the lines of an event ? To rephrase, where exactly might I use the above snippet ?
Just looking at options at places to do routine tasks when user performs operations on a list item.