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

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

2 Answers 2

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:

<XmlDocuments>
        <XmlDocument NamespaceURI="http://schemas.microsoft.com/sharepoint/events">
            <spe:Receivers xmlns:spe="http://schemas.microsoft.com/sharepoint/events">
                <Receiver>
                    <Name>YOURRECEIVERDISPLAYNAME</Name>
                    <Type>ItemUpdating</Type>
                    <SequenceNumber>10001</SequenceNumber>
                    <Assembly>ASSEMBLY, Version=1.0.0.0, Culture=neutral, PublicKeyToken=YOURTOKEN</Assembly>                                                <Class>RECEIVER CLASS</Class>
                </Receiver>
                <!-- repeat for other events like ITemAdded etc. -->
            </spe:Receivers>
        </XmlDocument>
    </XmlDocuments>

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: stackoverflow.com/questions/6215202/… 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

 
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.