Answered by:
attach programmatically an event receiver to an list

Question
-
Hey,
i have to attach programmatically an event receiver to an list. For this I found a good tutorial under:
http://bramnuyts.be/2011/04/01/add-an-event-receiver-to-a-specific-list-programmatically/
In this tutorial is this code:
const string _listName = "test"; const SPEventReceiverType _eventType = SPEventReceiverType.ItemAdded; public override void FeatureActivated(SPFeatureReceiverProperties properties) { SPWeb oWeb = (SPWeb)properties.Feature.Parent; oWeb.Lists[_listName].EventReceivers.Add(_eventType, Assembly.GetExecutingAssembly().FullName, "EventReceiverProject1.EventReceiver1.EventReceiver1"); }
So it works with fine for one event receiver type. But how I has to change the code when I like to ad more than one event receiver type (like ItemAdded and ItemUpdated)?
Thanks for your help
Stefan
Viele Grüße Stefan
Kontakt unter Info@IT-Kiessig.deFriday, February 1, 2013 7:22 PM
Answers
-
Here. I am assuming Your EventReceiver1 class have both ItemAdded and ItemUpdated defined.
oWeb.Lists[_listName].EventReceivers.Add(SPEventReceiverType.ItemAdded, Assembly.GetExecutingAssembly().FullName, "EventReceiverProject1.EventReceiver1.EventReceiver1"); oWeb.Lists[_listName].EventReceivers.Add(SPEventReceiverType.ItemUpdated, Assembly.GetExecutingAssembly().FullName, "EventReceiverProject1.EventReceiver1.EventReceiver1"); oWeb.Lists[_listname].Update();
Credit to Chris. I have simply provided code for what he already mentioned. :)
Amit
- Edited by Amit V Friday, February 1, 2013 8:25 PM
- Marked as answer by Stefan3110 Friday, February 1, 2013 9:16 PM
Friday, February 1, 2013 8:24 PM
All replies
-
-
Thanks Chris for your help.
Can you please give me a code example? So i can understand it better. thanks
Viele Grüße Stefan
Kontakt unter Info@IT-Kiessig.deFriday, February 1, 2013 7:53 PM -
Here. I am assuming Your EventReceiver1 class have both ItemAdded and ItemUpdated defined.
oWeb.Lists[_listName].EventReceivers.Add(SPEventReceiverType.ItemAdded, Assembly.GetExecutingAssembly().FullName, "EventReceiverProject1.EventReceiver1.EventReceiver1"); oWeb.Lists[_listName].EventReceivers.Add(SPEventReceiverType.ItemUpdated, Assembly.GetExecutingAssembly().FullName, "EventReceiverProject1.EventReceiver1.EventReceiver1"); oWeb.Lists[_listname].Update();
Credit to Chris. I have simply provided code for what he already mentioned. :)
Amit
- Edited by Amit V Friday, February 1, 2013 8:25 PM
- Marked as answer by Stefan3110 Friday, February 1, 2013 9:16 PM
Friday, February 1, 2013 8:24 PM -
Thanks for your help.
The code example helps me a lot.
In the eventreceiver1 class added und updated method is the same visual studio creates automatically when I add an event receiver to my solution.
When I activate the feature SharePoint hangs the method Itemupdated from the class as an event receiver on the list. Is this thing right? , then I thing I have understand it.
Sorry when I answers come a bit late. But I seat in the train and the internet connection is most of the time broken.
Viele Grüße Stefan
Kontakt unter Info@IT-Kiessig.deFriday, February 1, 2013 9:16 PM