Pages

Tuesday, August 14, 2012

Add Event Handler to Specific List or Library

Event Handlers in SharePoint is the de-facto choice if someone wants to perform some operation synchronously. Some would also point out about Workflows, though workflows can perform the same operation but workflow is acting asynchronously. Suppose you want to set the document or item metadata while adding you would have to use event handler or suppose you want to prevent somebody from deleting an item from a list or library you will be using event handlers. In SharePoint 2010 there are more event handler definition added compared to MOSS 2007. You can find these information here.

In this article let us take a look at how you can register event handlers for a specific list or library. We are focusing on registering it to specific list or library because by default event handlers get registered to all the list or library of particular type. For example if you specify "ListTemplateId=100" event receiver gets registered to all lists that are of type "Custom Lists" Often times you would want the event receiver to be registered to a particular list rather than all the lists in the site. Registering event receivers to a particular List or library give you the flexibility to define an operation specific to the list or library.

There are few different ways you can register event handlers for a list or library.

If you do not worry about event handler getting attached to all the list of a type but just want to control the operation for a specific list you register the event handler on a feature and by checking the List Title you can perform the operation for the specific List.You can use the List Title from properties to prevent event firing for other libraries or lists depending on your type of event receiver.

if(properties.List.Title == "your listname")
{
}

If  you are particular about the event handler getting attached to multiple list and  you like to take control of everything under you then you can use List eventreceiver property to set event receiver for your specific list or library. You can create a feature and in Feature activation you can register the event only for your list. The code below is an example that I use for setting the event handler for Shared Documents library in SharePoint
//This is a web feature

  public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            using (SPSite site = new SPSite(((SPWeb)properties.Feature.Parent).Url))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList sharedDocuments = web.Lists["Shared Documents"];
                    if (sharedDocuments != null)
                    {
                        SPEventReceiverDefinition listeventreceiver =  sharedDocuments.EventReceivers.Add();
                        listeventreceiver.Class = "TestEventReceivers.TestListEventReceiver";
                        listeventreceiver.Assembly = System.Reflection.Assembly.GetExecutingAssembly().FullName; // you can give the assembly name in string format.
                        listeventreceiver.SequenceNumber = 4000;
                        listeventreceiver.Type = SPEventReceiverType.ItemAdded;
                        listeventreceiver.Update();

                    }
                }
            }
        }
In FeatureActivated Method you need to check for existance of event receiver and if not you can add your event receiver. You need to have a class defined for your event receiver.
Event Receiver Example
 public class TestListEventReceiver : SPItemEventReceiver
    {
        public override void ItemAdded(SPItemEventProperties properties)
        {
            base.ItemAdded(properties);
        }
    }

The above operation is similar for assigning event receiver to content type, then use the event in any library or list it is used.

Another way you could assign Event Receiver is by using the list URL. This is a nice and handy way if you are deploying a solution as part of your product or offering and you are creating a List Instance using <ListInstance> in your feature and have defined URL or you already have created the list manually in a site and you have the List URL.

When you create Event Receivers in your solution in Visual studio you will have the following line added

<Receivers ListTemplateId="101">

Replace the List template in the above line as follows. Now you have List instance specific event receiver added.

<Receivers ListUrl="Lists/MyDocuments">
 
I am assigning to a Library in the above example.