Welcome to MSDN Blogs Sign in | Join | Help

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;
                    }
                }
            }
        }

Published Tuesday, April 01, 2008 9:20 AM by ketaanhs

Comments

# Links (4/1/2008) &laquo; Steve Pietrek&#8217;s SharePoint Stuff

Tuesday, April 01, 2008 9:07 PM by Links (4/1/2008) « Steve Pietrek’s SharePoint Stuff
Anonymous comments are disabled
 
Page view tracker