Attach Event Receiver to Specific List Programmatically in SharePoint 2010

Recently, developed an Event Receiver to set Item Level permissions based on the list field “Visible to Visitors”. Event receiver binds with all document libraries using ListTemplateId element in Elements.xml.  Event Receiver to Set Item Level Permissions based on List Column Value

How to Associate an Event Receiver with a List Programmatically in SharePoint?

SharePoint 2010 offers ListURL to be specified in Elements.xml file to attach the Event Handler to the particular list. MOSS 2007 doesn’t! So you have to write code to register, Maybe Console application/Feature Receiver, etc.

Code to Register Event Receiver with a Particular List:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;

namespace SetItemLevelPermission
{ 
    //Class to Register Event Receiver to particular List

    class BindEventHandlerToLists : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
              SPWeb  web  =  (SPWeb)properties.Feature.Parent;

              //Get the Lists in which the Event receiver to be attached. We can Get this from Config file instead of hard coding
              //Check whether the List exists!
              SPList list = web.Lists["Project Documents"];

              //Set the Assembly, Class names
              string AssemblyName = "SetItemLevelPermission, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3e166fe20cd991fe";
              string ClassName = "SetItemLevelPermission.SetPermissions";

              //Register the event handlers
              list.EventReceivers.Add(SPEventReceiverType.ItemAdded, AssemblyName, ClassName);
              list.EventReceivers.Add(SPEventReceiverType.ItemUpdating, AssemblyName, ClassName);
              
            list.Update();

        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWeb web = (SPWeb)properties.Feature.Parent;
            SPList list = web.Lists["Project Documents"];

            // Remove the Binding of the Event Handler from Lists
            string AssemblyName = "SetItemLevelPermission, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3e166fe20cd991fe";

            //Delete the Event Receivers
            for(int i=list.EventReceivers.Count-1; i>=0; i--)
            {
                if (list.EventReceivers[i].Assembly == AssemblyName)
                {
                    list.EventReceivers[i].Delete();
                }
            }

        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
            //throw new Exception("The method or operation is not implemented.");
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            //throw new Exception("The method or operation is not implemented.");
        }
    }
}

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

Leave a Reply

Your email address will not be published. Required fields are marked *