Delete eventhandler bound to the list in Moss

Source: Internet
Author: User
In Moss, we usually add many eventhandler events to the list. Program But there is always a problem, that is, how to remove these eventhandler. I searched the internet and did not find a good solution. Instead, I deleted the DLL from GAC, or re-peat an empty assembly, but I always think that this method is the best way to do this. It is a relatively "soil" (in case you don't remember the name ...), Haha, I suddenly got an idea from a foreigner's blog. I can add this DLL in another way. The advantage of this method is that I can assign a guid to this DLL, in this way, we can find it and delete it.
Generally, we use the following method to register an eventhandler event handler for the List (this method is also used in the early fat brother tutorial ):
Spsite site = new spsite (siteurl );
Site. openweb (weburl );
Splist list = web. Lists [list name];

List. eventreceivers. Add (event type, assembly name, class name );
Each splist object has an eventreceivers set. The eventreceivers set exposes an add method with three different overload implementations. This add method is the simplest way to add eventhandler. It requires three parameters to be passed. If you wantCodeTo bind an event handler, you must at least provide the Assembly name, The namespace-defined class name, and the event type that uses the speventreceivertype enumeration from The WSS object model. Calling the add method is the simplest method, but it is not flexible in all circumstances.

The following code illustrates a more complex method, in which the event handler is bound by directly programming the speventreceiverdefinition object. In this way, you have the opportunity to specify a specific guid identifier for the binding, so that you can easily add and delete them. It also enables you to initialize the sequencenumber attribute and data attribute.
Splist salesleadslist = web. Lists [list name];
Speventreceiverdefinitioncollection receivers;
Receivers = salesleadslist. eventreceivers;
Guid itemaddingdefid = new GUID ();
Itemaddingdefid = new GUID ("2d96adce-e7a9-461b-8db6-1e1_9419503"); // specify the guid
If (! Receivers. eventreceiverdefinitionexist (itemaddingdefid ))
{
Speventreceiverdefinition itemaddingdef;
Itemaddingdef = receivers. Add (itemaddingdefid );
Itemaddingdef. Name = "itemdeleteinghandler ";
Itemaddingdef. type = event type;
Itemaddingdef. Assembly = Assembly name;
Itemaddingdef. Class = Class Name;
Itemaddingdef. Data = "today ";
Itemaddingdef. Sequence Number = 1001;
Itemaddingdef. Update ();
}
In this way, a guid is specified for the Assembly and deleted through the guid.
If (receivers. eventreceiverdefinitionexist (itemaddingdefid ))
{
Receivers [itemaddingdefid]. Delete ();
}

Summary: eventhandler is simply uninstalled from the GAC. When you add or delete an update, the list will definitely find this eventhandler, which is definitely different from the speed of the previous method, it is similar to hiding something on a page or deleting something (I believe many colleagues have done this before modifying the homepage or template page.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.