19

In an event receiver, is it necessary to call base.ItemAdding(properties);, base.ItemUpdating(properties);, etc. at the beginning of your event handler?

I've seen people arguing that it isn't necessary because it doesn't do anything, but it seems to be in an awful lot of code samples.

1 Answer 1

15

So I was intregued as to the actual answer as I have always left it in. I guess one of the reasons why is that I think its good practice and hasn't caused me any problems. This plus Visual Studio puts it in when you override the function.

However, it looks like you should leave it in as when reflected this is what's implemented underneath:-

public virtual void ItemAttachmentAdded(SPItemEventProperties properties) { this.BaseItemEventReceiver(properties); } 

which calls into the function BaseItemEventReceiver(properties) which looks like this:-

private void BaseItemEventReceiver(SPItemEventProperties properties) { if (properties == null) { throw new ArgumentNullException("properties"); } properties.Status = SPEventReceiverStatus.Continue; } 

I would suggest from the code in BaseItemEventReceiver() that you should call the base class function first to validate the SPItemEventProperties object before going through your code.

1
  • 5
    It's interesting though that if the properties are null that it doesn't set the Status property to CancelWithError while providing an ErrorMessage, rather than throwing an ArgumentNullException. So I suppose if you wanted to handle this more elegantly you could just check for a null reference yourself, rather than calling the base method. Commented Feb 8, 2013 at 14:54

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.