How to un-register an Event Handler on a List, sharepoint 2007, MOSS


Best way to deploy your event handlers in Production environment is thru Solutions/Features. All you need to use is Feature Assembly and basically use Feature Activated and Feature Deactivating events.


Following is the code that you might want to refer on Feature Activated:


    {


            SPWeb web = SPContext.Current.Web;
            SPList spListName = web.Lists["Project Time Entry List"];


            string assemblyName = "AssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=36f7c742d410fd5b";
            string className = "AssemblyName.TotalCheck_ItmEvntHdlr";
            SPEventReceiverDefinition evntRcvr = spListName.EventReceivers.Add();
            evntRcvr.Name = "TotalCheck_ItemAdding";
            evntRcvr.Type = SPEventReceiverType.ItemAdding;
            evntRcvr.SequenceNumber = 200;
            evntRcvr.Assembly = assemblyName;
            evntRcvr.Class = className;
            evntRcvr.Update();


            SPEventReceiverDefinition evntRcvr_updt = spListName.EventReceivers.Add();
            evntRcvr_updt.Name = "TotalCheck_ItemUpdating";
            evntRcvr_updt.Type = SPEventReceiverType.ItemUpdating;
            evntRcvr_updt.SequenceNumber = 201;
            evntRcvr_updt.Assembly = assemblyName;
            evntRcvr_updt.Class = className;
            evntRcvr_updt.Update();
    }


Following is the code that you can use in the Feature Deactivating code


        {
            SPWeb web = SPContext.Current.Web;
            SPList spListName = web.Lists["Project Time Entry List"];
           
            for(int i=0; i<spListName.EventReceivers.Count;i++)
            {


                if (spListName.EventReceivers[i].Name != null)
                {
                    if (spListName.EventReceivers[i].Name == "TotalCheck_ItemAdding" || spListName.EventReceivers[i].Name == "TotalCheck_ItemUpdating")
                    {
                        spListName.EventReceivers[i].Delete();


                       i = -1;
                    }
                }
            }
        }

Comments (2)

  1. Sushant says:

    How I can attach Event handler to Specific List in MOSS 2007, I tried Most of the Option using Programmatically  but no Success.

    I have refered this link "msdn.microsoft.com/…/microsoft.sharepoint.speventreceiverdefinition.aspx" but not able to find required class or name space for  eventReceiver.Synchronization =

            SPEventReceiverSynchronization.Synchronous

Skip to main content