SharePoint Event Handlers

A while back, I found myself in a situation where I assumed MOSS Document Libraries were capable of creating thumbnails from images.  After all, MOSS creates thumbnails of images for Picture libraries.  It turns out this was a bad assumption.  This feature had been promised to a customer, so I had to find a solution.

Enter MOSS Event Handlers.  Event Handlers allow developers to hook into the MOSS event model and run custom code when MOSS performs a specific action. In my case, I used the SPItemReceiver class to create a thumbnail of an image when it was added to a document library.  There are Event Handlers for Sites, Lists and List Items.

Here is a short sample of EventHandler code that will override the ItemAdded, ItemUpdate and ItemDeleting methods.

using Microsoft.SharePoint;
namespace ImageSource.EventHandlerExample
{
  public class ListItemEventHandler : SPItemEventReceiver
  {
    public override void ItemAdded(SPItemEventProprties properties)
    {
        //Called when an item is added to a list.
    }
    public override void ItemUpdated(SPItemEventProperties properties)
    {
      //Called when an item is updated, this method is also called with an item is added.
    }
    public override void ItemDeleting(SPItemEventProperties properties)
    {
        //Called when an item is deleted from the list.
    }
  }
}


For my solution, the ItemAdded method grabs the image that was uploaded, creates a thumbnail, and adds the new thumbnail rendition to another library that shadows the library with the pictures.  ItemDeleting removes the thumbnail from the 'shadow' library. ItemUpdated is empty, but I left the method in my code in case it was needed at a later time.

Hopefully, this example gives you an idea of how SharePoint can be extended using Event Handlers.  The ability to hook into SharePoint's event model is quite powerful.  I wish more of the ECM systems I work with had a similar feature.

Tyson Magney
Senior Developer
ImageSource, Inc.

Share on LinkedIn   Share on Twitter

Follow

Get every new post delivered to your Inbox.