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.

 

2 thoughts on “SharePoint Event Handlers

  1. hi tyson,

    i regularly read your blog. I have a simple question as I am divining into sharepoint technology. I have a requirement when user upload a file then sharepoint should automatically render it to pdf/a.

    1. i found a dll that can convert different files into pdf/a.
    2. i was wondering how can use this dll in sharepoint so when user uploads a file it renders to pdf/a

    a. i was thinking using itemadded event handler.
    b. but I have NO idea how to use dll with this event handler…
    c. I think i have to register the dll in GAC. dont know how either.

    any idea??

    JAF

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s