Walkthrough: Add List Event Receiver dynamically at runtime in SharePoint 2010


This time a tiny neat walkthrough of how to add an Event Receiver at runtime in SharePoint 2010.

Let’s say you have a SharePoint site that your colleagues already use. In this site you have an existing list. Now you want to add some automation to this existing list. – You cannot deploy the list as List Definition w/ List Instance again in a VS 2010 SharePoint project, because the list exists and the data must not be touched.

One solution is to add an List Event Receiver that is contained in a VS2010 solution package.

1. You create a Empty SharePoint 2010 project in Visual Studio 2010.

2. Now you add an “Event Receiver” project item

image

3. Now you add the events you want to handle. Select “List Item Events” and “Custom List”.

image

4. Implement some functionality in the newly created Event Receiver class.

5. Now create or open an Feature Event Receiver for the SharePoint feature that will configure the event receiver. – You have to create a new feature or use an existing feature… If you create a new feature event receiver you have to uncomment the methods “FeatureActivated” and “FeatureDeactivating”.

6. Add this code to the “FeatureActivated” method:

try {
    SPWeb web = (SPWeb)properties.Feature.Parent;
    SPList l = web.Lists["My SharePoint List"];
    if( l != null )
    {
        bool found = false;
        foreach( SPEventReceiverDefinition er in l.EventReceivers )
        {
             if( er.Class == "Full.Namespace.Qualified.Class.Name.Of.Your.Event.Receiver.Class")   {
                 found = true;
                 break;
             }
        }

        if( !found )
        {
            SPEventReceiverDefinition newERD = l.EventReceivers.Add();

//the next line is only valid if the event receiver class is in the same assembly as the feature event receiver!!!

            newERD.Assembly = System.Reflection.Assembly.GetExecutingAssembly().FullName;
            newERD.Class = "Full.Namespace.Qualified.Class.Name.Of.Your.Event.Receiver.Class";
            newERD.SequenceNumber = 1000;

//you may add more “received” events in the following line.

            newERD.Type = SPEventReceiverType.ItemUpdated | SPEventReceiverType.ItemAdded;
            newERD.HostId = l.ID;
            newERD.HostType = SPEventHostType.List;
            newERD.Update();
            l.Update();
        }
    }
}
catch {
}

This installs the event receiver when the feature gets activated.

7. Add this code to the “FeatureDeactivating” method:

try {
    SPWeb web = (SPWeb)properties.Feature.Parent;
    SPList l = web.Lists["My SharePoint List"];
    if( l != null )
    {
        SPEventReceiverDefinition d = null;
        foreach( SPEventReceiverDefinition er in l.EventReceivers )
        {
            if( er.Class == "Full.Namespace.Qualified.Class.Name.Of.Your.Event.Receiver.Class" )
            {
                d = er;
                break;
            }
        }

        if( d != null )
        {
            d.Delete();
            l.Update();
        }
    }
}
catch {
}

This will remove the event receiver when the feature gets deactivated.

8. Now remove the “Elements.xml” file in the Event Receiver project item in the Solutions Explorer:

image

9. For me this works very well.

Advertisements

7 thoughts on “Walkthrough: Add List Event Receiver dynamically at runtime in SharePoint 2010

  1. Isnt it posible to combine 2 types into one receiver? or must i add 2 receiver objects to the same list if i want adding and updating?
    The chain with “ItemAdded | ItemUpdated” wont work for me 😦

    Thx for this tutorial!

  2. Hi ikarstein,

    Thanks for the nice walkthrough. This was exactly what I needed!
    But since I had multiple event receivers to add/delete, I added two following two methods to the feature:

    private void AddEventReceiver(SPList list, string name, string className, SPEventReceiverType type)
    {
    if (list.EventReceivers.Cast().Where(e => e.Name.Equals(name)).ToList().Count == 0)
    {
    SPEventReceiverDefinition eventReceiver = list.EventReceivers.Add();
    eventReceiver.Name = name;
    eventReceiver.Assembly = System.Reflection.Assembly.GetExecutingAssembly().FullName;
    eventReceiver.Class = className;
    eventReceiver.SequenceNumber = 2001;
    eventReceiver.Type = type;
    eventReceiver.HostId = list.ID;
    eventReceiver.HostType = SPEventHostType.List;
    eventReceiver.Update();
    }
    }

    private void DeleteEventReceivers(SPList list, string className)
    {
    var eventReceivers = list.EventReceivers.Cast().Where(e => e.Class.Equals(className)).ToList();
    foreach (SPEventReceiverDefinition eventReceiver in eventReceivers)
    {
    eventReceiver.Delete();
    list.Update();
    }
    }

    Cheers,
    Mathijs Vlasveld

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s