I'm currently implementing an Event Handler for a Picture Library - a project I will blog about in the near future - and have learned the hard way about handling event handlers...
The issue I am experiencing applies to WSS 3.0 (and MOSS 2007) and seems to be about the pre-event event types. The pre-event event handlers are triggered synchronously and allow to cancel the event based on some own business logic and return a message to the user.
All available Event Types can be found here: SPEventReceiverType Enumeration (Microsoft.SharePoint). The pre-event event types are the ones ending with "-ing".
Issue
Consider the following implementation of the ItemAdding event:
public override void ItemAdding(SPItemEventProperties properties)
{
try
{
// DO SOMETHING
}
catch (Exception ex)
{
properties.Cancel = true;
properties.ErrorMessage = ex.Message;
// LOG ERROR
}
}
Register this event handler to a standard Picture Library. You now have an enhanced Picture Library where you can decide which files can be added to the library.
If you test this functionality by uploading a picture, you will notice that there is no thumbnail generated.
Solution, or not ?
public override void ItemAdding(SPItemEventProperties properties)
{
try
{
// DO SOMETHING
}
catch (Exception ex)
{
properties.Cancel = true;
properties.ErrorMessage = ex.Message;
// LOG ERROR
}
base.ItemAdding(properties);
}
The above code seems to fix the thumbnail generation but our error mesage does not show to the user anymore. (Note that my error logging still works as expected).
The above led me to believe that I can't unconditionally do a base.ItemAdding(properties), so I decided to only do it when no error was thrown.
public override void ItemAdding(SPItemEventProperties properties)
{
try
{
// DO SOMETHING
base.ItemAdding(properties);
}
catch (Exception ex)
{
properties.Cancel = true;
properties.ErrorMessage = ex.Message;
// LOG ERROR
}
}
However now the thumbnail generation stops working again.
Conclusion
No real conclusion here, it just seems like there are still issues with Synchronous Event Handling (Pre-Events) in WSS 3.0 or maybe it is just the Picture Library that's misbehaving ?
Is it required to call the base method when overriding an event handler method ?
Is this a bug that will be solved in SP1 and if so, is there a workaround or hotfix ?
For now it seems only safe not to use the Cancel and ErrorMessage properties...