sharepointsharepoint-2010event-receiver

How do I create an event receiver for a single list that is based on a custom content type?


I am trying to create an event receiver for a list I have created called Questions.

When a new question is added I want an event to fire. I have looked into it so I know I need to create an event receiver and make use of the ItemAdded method.

How do I bind this to one instance of a list and what do I select for the Source type when creating the receiver? I don't have an option for "Custom List" in there as some blog posts suggest.

Hope someone can help..


Solution

  • I think this might be what you're after: (http://msdn.microsoft.com/en-us/library/ff407249.aspx)

    using (SPSite site = new SPSite("http://localhost")) 
    {
        using (SPWeb web = site.OpenWeb())
        {
            SPList list = web.Lists["Shared Documents"];
    
            SPEventReceiverDefinition def = list.EventReceivers.Add();
    
            def.Assembly = "ERDefinition, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=704f58d28567dc00";
            def.Class = "ERDefinition.ItemEvents";
            def.Name = "ItemAdded Event";
            def.Type = SPEventReceiverType.ItemAdded;
            def.SequenceNumber = 1000;
            def.Synchronization = SPEventReceiverSynchronization.Synchronous;
            def.Update();
        }
    }
    

    Regards,

    joel

    --

    http://joelblogs.co.uk

    @joelblogs