SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have an event handler for the ItemAdding event of a document library. However, I cannot get it to handle the event unless I register the event handler using the Sharepoint Event Handler Manager. This is fine on a development server, but moving to production, I'd like to be able to register it programatically. Is there a way to do this without using the Sharepoint Event Handler Manager?

Thanks in advance!

share|improve this question
up vote 1 down vote accepted

You can create a web-scoped feature with a feature receiver like this:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
  using (SPWeb web = properties.Feature.Parent as SPWeb)
    SPList myList = web.Lists["My List"]; 
    if (myList != null)
      string assemblyName = "MyAssembly, [full 4-part name]";
      string className = "MyAssembly.MyItemEventHandler";
      myList.EventReceivers.Add(SPEventReceiverType.ItemAdding, assemblyName, className);

You should then remove the event receiver when the feature is deactivated.

share|improve this answer
Thanks Laurie. I have to create a whole different feature? I can't bundle this in with my SPItemEventReceiver feature? – Music Magi Jun 9 '11 at 20:12
Yes, a feature receiver is different from an item event receiver. The feature receiver executes when a feature is activated by the user under site actions>site settings>manage site features. – Laurie Jun 9 '11 at 20:25
Okay. Thanks again! – Music Magi Jun 9 '11 at 20:38
Ok, so I've created a feature receiver class that inherits from SPFeatureReceiver. The code assigns the event receiver to my list. I'm a little confused on how the manifest and feature files are set up. Please take a look at my latest post if you can provide any insight on this. thanks! – Music Magi Jun 10 '11 at 15:19

In 2007, you can do this declaratively as well in your content type definition:

        <XmlDocument NamespaceURI="">
            <spe:Receivers xmlns:spe="">
                    <Assembly>ASSEMBLY, Version=, Culture=neutral, PublicKeyToken=YOURTOKEN</Assembly>                                                <Class>RECEIVER CLASS</Class>
                <!-- repeat for other events like ITemAdded etc. -->

P.S: It works in 2010 as well but only for types that have INHERIT set to false

share|improve this answer
In the same file as my <Elements>? <Elements><Receivers><Receiver> etc.. – Music Magi Jun 9 '11 at 20:51
yes, inside the <ContentType tag, under the fieldrefs – Colin Jun 9 '11 at 21:01
ContentType tag? You can see what my files look like here:… I'm not sure what you're referring to – Music Magi Jun 10 '11 at 13:50
This code would go in your element manifest file for your content type definition. From your question, though I don't see that you have defined a content type for your document library. So, you would either need to define a content type and attach your event receiver declaratively as defined here, or use code (e.g. in a feature receiver) to attach your ItemAdding event receiver to your specific list. – Laurie Jun 10 '11 at 16:23
The content type gets created in the code. – Music Magi Jun 13 '11 at 15:21

Your Answer


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.