Here is code sample for Attaching Event Receiver to particular list / multiple list on Feature activation :
For single list :
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
try
{
SPWeb oWeb = properties.Feature.Parent as SPWeb;
SPList M1list = oWeb.Lists.TryGetList("M1List");
if (M1list != null)
{
M1list.EventReceivers.Add(SPEventReceiverType.ItemAdding, Assembly.GetExecutingAssembly().FullName, "GlobalSubmit.Intranet.DMParentFolder");
M1list.EventReceivers.Add(SPEventReceiverType.ItemUpdating, Assembly.GetExecutingAssembly().FullName, "GlobalSubmit.Intranet.DMParentFolder");
}
}
catch (Exception ex)
{
throw ex;
}
}
For Multiple list:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
try
{
SPWeb oWeb = properties.Feature.Parent as SPWeb;
SPList M1list = oWeb.Lists.TryGetList(Constants.M1List);
if (M1list != null)
{
M1list.EventReceivers.Add(SPEventReceiverType.ItemAdding, Assembly.GetExecutingAssembly().FullName, "Class path of event receiver");
M1list.EventReceivers.Add(SPEventReceiverType.ItemUpdating, Assembly.GetExecutingAssembly().FullName, "Class path of event receiver");
}
SPList M2list = oWeb.Lists.TryGetList(Constants.M2List);
if (M2list != null)
{
M2list.EventReceivers.Add(SPEventReceiverType.ItemAdding, Assembly.GetExecutingAssembly().FullName, "Class path of event receiver");
M2list.EventReceivers.Add(SPEventReceiverType.ItemUpdating, Assembly.GetExecutingAssembly().FullName, "Class path of event receiver");
}
}
catch (Exception ex)
{
throw ex;
}
}
In this code class path is the only thing you need to worry about. If you pass it wrong then you will not be able to attach event receiver to list\library. Here is an example of class path:
If my solution name is "example.intranet" and my event receiver class name is "demoeventreceiver.cs" then my class path will be- "example.intranet.demoeventreceiver".
For Deactivation Feature Click Here To See the code.
Subscribe to:
Post Comments (Atom)
What is the Use of X and Y Properties in Power Apps SharePoint List Form?
If you've spent any time customizing SharePoint List Forms using Power Apps, you've likely come across the X and Y properties ...
-
Code Review :- General 1. Remove the commented code 2. Variable name should not be contain "_". 3. Query should be in separat...
-
In last blog we learn how can we enable footer on SharePoint Online Modern Communication site. If you have not gone through that you can use...
-
Issue : Recently, we had a requirement to sync the calendar between shared mailbox outlook and SharePoint Calendar list. We have created 2 f...
No comments:
Post a Comment