Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 7 down vote accepted

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.

share|improve this answer
2  
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. –  RJ Cuthbertson Feb 8 '13 at 14:54
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.