The "List event receivers" are also defined as event handlers for the list. These events are triggered when something gets changed in the SharePoint list items and become very useful when a user wants to access the list item data using events before and after properties to do some important tasks. I thought of writing this blog to share my experience with the process of creating/deleting these list event receivers and also to mention some important points to remember while dealing with them. Some of the different types of event receivers which can be attached to the list are:
- Item Adding: This event occurs when a user is creating a new item in the list.
- Item Added: This event occurs after the user has created a new item in the list.
- Item Updating: This event occurs at the time when the data in the list is the state of modification.
- Item Updated: This event occurs after the data in the list has been modified.
- Item Deleting: This event occurs when the user is in the process of deleting an item in the list.
- Item Deleted: This event occurs after the user has deleted an item in the list.
Adding an Event Receiver
The event receivers contain the event information and the item information on which the event occurred. The event receivers can be attached to the list in the following manner:
list.EventReceivers.Add( SPEventReceiverType, assemblyName, sClassName);
Where the different parameters are:
- List - This is the list to which you want to attach the event receiver.
- SPEventReceiverType - The first parameter would be the type of event receiver you want to attach. In the example above, the event type is ItemAdded.
- assemblyName - This would be the name of the assembly containing the class which overrides the event receiver class of the list, e.g.:
private const string assemblyName = "ListEventReceiver, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3dc9djfae2d341bf";
- sClassName - This variable defines the name of the class which overrides the existing event receiver class( SPItemEventReceiver) methods for the list e.g.
private const string sClassName = "ListEventReceiver.EventReceiver";
Important points to remember in the process of adding an event receiver to the list:
- The assembly should present in the GAC.
- The referenced assembly should have a strong name i.e. sign an assembly before using it.
- Update the list to which you have attached the event receiver using list.Update();
The SPItemEventReceiver base class provides methods for event receivers in the Windows SharePoint Services object model and this class is never instantiated. The overriding methods can disable the firing of some other event while this event handler method is in processing using SPEventReceiverBase.DisableEventFiring (). This method will prevent events from being raised and also the infinite loop.
OR
In detail, I have a list which has an ItemUpdated Event Receiver (Custom one, which developed by the developer). On the event receiver, we are trying to update some of the columns based on the entered column value. i.e., based on Column1, I am calculating the value for the Column2 and update the list item on the Event Receiver. In that case, what happens is, again and again, the Event Receiver triggers as ends up with a bottle neck
OR
Normally you disable / enable eventfiring in you ItemUpdated or ItemAdded to prevent recursive calls of the event receiver. So it only prevents events from getting raised in between your code blocks Disabling and Enabling. After ItemUpdated have been finished working the next event receiver regarding the sequence number will be raised.
It's only event receiver internaly and you have to make sure if EvenFiring is diabled that the enabling code will get hit. Otherwise it will disable the event on some items.
Follow the code below to override the event receiver method:
public class EventReceiver : SPItemEventReceiver
{
public override void ItemUpdating(SPItemEventProperties properties)
{
try
{
//Stop other events from firing while this method executes
this.DisableEventFiring();
//Add your code here
}
catch (Exception ex)
{ }
finally
{ //Re-enable event firing
this.EnableEventFiring();
}
}
}
When user needs to access the data associated with event receivers, they access it using event receiver properties. The SPItemEventProperties contains the after and before property of an item which can be used to access the item value before/after the change occurred. The event receiver property also contains an important field called ReceiverData which can be used to store the custom data. This data can be used to pass any information which is not present in the properties itself.
The table below shows the values contained in the SPItemEventProperties properties when the specific event occurs for the list:
List | properties.ListItem (Before property value) | Properties.afterProperties |
ItemAdding | Null | New Value |
ItemAdded | New Value | New Value |
ItemUpdating | Old Value | New Value |
ItemUpdated | New Value | New Value |
ItemDeleting | Old Value | Null |
ItemDeleted | Null | Null |
In the table above:
- Properties.ListItem values denotes the item value before the event occurred
- AfterProperties denote the values of item after event occurred.
Deleting an Event Receiver
The event receiver can be deleted from the attached list in the following manner:
list.EventReceivers[eventReceiverGuid].Delete();
or
list.EventReceivers[x].Delete();
Where the different parameters are:
- List - This is the list to which you want to attach the event receiver.
- eventReceiverGuid - This denotes the GUID of the specific type of event receiver.
- x- This i denotes the position of the event receiver in the list of event receivers.
The important points to remember while deleting the event receivers:
- Allow the safe updates on the web using list.ParentWeb.AllowUnsafeUpdates = true;, as it is required to make any deletion to the list.
- Update the list after you delete an event receiver using list.Update();
- Finally in the end make the list.ParentWeb.AllowUnsafeUpdates = false; to prevent any security risks.
No comments:
Post a Comment