EventHander添加删除也许应该注意的

     public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            try
            {
                using (SPSite site = new SPSite(((SPSite)properties.Feature.Parent).Url))
                {
                    string assembleName = "Demetersoft.KipForMoss.WorkFlow.Feature.EventHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=486e336fb7c97750";
                    string EventHandlerNameSpace = "Demetersoft.KipForMoss.WorkFlow.Feature.EventHandler.";
                    string HQReceiveDocLib_ItemUpdatedName = "HQReceiveDocLib_EventHandler";
                    string HQIssueDocLib_ItemUpdatedName = "HQIssueDocLib_EventHandler";

                    SPList HQReceiveDocLib = site.RootWeb.Lists["总部收文库"];
                    SPList HQIssueDocLib = site.RootWeb.Lists["总部发文库"];

                    for (int i = 0; i < HQIssueDocLib.EventReceivers.Count; i++)
                    {
                        SPEventReceiverDefinition receiver = HQIssueDocLib.EventReceivers[i];
                        if (receiver.Class.Equals(EventHandlerNameSpace + HQReceiveDocLib_ItemUpdatedName))
                        {
                            receiver.Delete();
                        }
                    }

                    for (int i = 0; i < HQIssueDocLib.EventReceivers.Count; i++)
                    {
                        SPEventReceiverDefinition receiver = HQIssueDocLib.EventReceivers[i];
                        if (receiver.Class.Equals(EventHandlerNameSpace + HQIssueDocLib_ItemUpdatedName))
                        {
                            receiver.Delete();
                        }
                    }                
                    HQReceiveDocLib.EventReceivers.Add(SPEventReceiverType.ItemUpdated, assembleName, EventHandlerNameSpace + HQReceiveDocLib_ItemUpdatedName);
                    HQIssueDocLib.EventReceivers.Add(SPEventReceiverType.ItemUpdated, assembleName, EventHandlerNameSpace + HQIssueDocLib_ItemUpdatedName);
                }
            }
            catch (Exception ex)
            {
                //异常处理
                return;
            }


        }


首先要清除认识的是 一个相同的事件可能要多个,并且都会运行
最好采取程序集+类名的 SPEventReceiverDefinition.Class来删除
因为你可能部署多个相同的时间句柄,防止误删别的

 

控制台测试主代码


        public static void ActionToFeatureReceiver(SPSite site)
        {

            using (SPWeb RootWeb = site.RootWeb)
            {
                SPList HQDispatchList = RootWeb.Lists["MytestFolder"];

                SPEventReceiverDefinitionCollection receiverCol = HQDispatchList.EventReceivers;

                string assembleName = "Demetersoft.KipForMoss.WorkFlow.Feature.EventHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=486e336fb7c97750";
                string EventHandlerNameSpace = "Demetersoft.KipForMoss.WorkFlow.Feature.EventHandler.";
                string HQReceiveDocLib_ItemUpdatedName = "HQReceiveDocLib_EventHandler";
                string HQIssueDocLib_ItemUpdatedName = "HQIssueDocLib_EventHandler";


                for (int i = 0; i < HQDispatchList.EventReceivers.Count; i++)
                {
                    SPEventReceiverDefinition receiver = HQDispatchList.EventReceivers[i];
                    if (receiver.Class.Equals(EventHandlerNameSpace + HQReceiveDocLib_ItemUpdatedName))
                    {
                        receiver.Delete();
                    }
                }

                HQDispatchList.EventReceivers.Add(SPEventReceiverType.ItemUpdated, assembleName, EventHandlerNameSpace + HQReceiveDocLib_ItemUpdatedName);
                HQDispatchList.EventReceivers.Add(SPEventReceiverType.ItemUpdated, assembleName, EventHandlerNameSpace + HQIssueDocLib_ItemUpdatedName);
              

                for (int i = HQDispatchList.EventReceivers.Count; i > 0; i--)
                {
                    SPEventReceiverDefinition receiver = HQDispatchList.EventReceivers[i - 1];
                    Console.WriteLine(receiver.Class);
                    Console.WriteLine(receiver .GetType ().BaseType);
                    Console.WriteLine(receiver.Type.ToString());
                    Console.WriteLine("======================");
              
                }

                Console.ReadLine();
            }
        }

 

输出:
Demetersoft.KipForMoss.WorkFlow.Feature.EventHandler.HQReceiveDocLib_EventHandler
System.Object
ItemUpdated
======================
Demetersoft.KipForMoss.WorkFlow.Feature.EventHandler.HQIssueDocLib_EventHandler
System.Object
ItemUpdated
======================

 

 

 

你可能感兴趣的:(event)