Sharepoint lets us tap into quite a substantial number of List Events. Some of them are:
SPEventReceiverType.FieldAdded;
SPEventReceiverType.FieldAdding;
SPEventReceiverType.FieldDeleted;
SPEventReceiverType.FieldDeleting;
SPEventReceiverType.FieldUpdated;
SPEventReceiverType.FieldUpdating;
SPEventReceiverType.InvalidReceiver;
SPEventReceiverType.ItemAdded;
SPEventReceiverType.ItemAdding;
SPEventReceiverType.ItemAttachmentAdded;
SPEventReceiverType.ItemAttachmentAdding;
SPEventReceiverType.ItemAttachmentDeleted;
SPEventReceiverType.ItemAttachmentDeleting;
SPEventReceiverType.ItemCheckedIn;
SPEventReceiverType.ItemCheckedOut;
SPEventReceiverType.ItemCheckingIn;
SPEventReceiverType.ItemCheckingOut;
SPEventReceiverType.ItemDeleted;
SPEventReceiverType.ItemDeleting;
SPEventReceiverType.ItemFileConverted;
SPEventReceiverType.ItemFileMoved;
SPEventReceiverType.ItemFileMoving;
SPEventReceiverType.ItemUncheckedOut;
SPEventReceiverType.ItemUncheckingOut;
SPEventReceiverType.ItemUpdated;
SPEventReceiverType.ItemUpdating;
Listening to an event is very easy, simply create a new class file, and create a class that derieves from the 'SPItemEventReceiver' class as shown below:
namespace SaturnVibes.EventsExample
{
public class ListEvents : SPItemEventReceiver
{
static ListEvents()
{
// Add Static constructor code here....
}
public override void ItemAdding(SPItemEventProperties properties)
{
// Add Code to execute when an Item is being added
}
public override void ItemAdded(SPItemEventProperties properties)
{
// Add Code to execute after an Item has being added
}
public override void ItemUpdating(SPItemEventProperties properties)
{
// Add Code to execute when an Item is being updated
}
}
}
In the above code sample, we have created a class in which the list's 'ItemAdding', 'ItemAdded' and the 'ItemUpdating' events were overriden. We can now execute our code in these list events.
The SPItemEventProperties object, will give us the properties that the listitem will have, both before and after the the event. An important thing to note here from the above sample is that, in an 'ItemAdding' event, since the List Item has not yet been created, 'properties.ListItem' will be null. To retrieve the values that a user may have entered while creating a list item, use 'properties.AfterProperties["Internal Name of the List Column"]'. Remember to provide the internal name of the List column inside the '[]'. (See how to get the Internal name of a List Column)
If the List event needs to be cancelled, we can set the 'properties.Cancel' to true and provide an error message in the 'properties.ErrorMessage' property.
Eg:
properties.ErrorMessage = "Your error message";
properties.Cancel = true;
Before the above code can be used however, the List event has to be registered to look for our code sample whenever an event is fired. This is done by:
SPWeb spWeb = new SPSite("URL of Site").OpenWeb();
list = spWeb.Lists["List Name"];
list.EventReceivers.Add(eventType, assemblyName, className);
where,
eventType is an enum of 'SPEventReceiverType'. Eg: 'SPEventReceiverType.ItemAdding'
assemblyName is the entire name of the assembly
className is the name of the class containing the event reciever. Eg: SaturnVibes.EventsExample.ListEvents
Happy Sharepointing :)