Wednesday, November 17, 2010

SharePoint List Event Handler

One of the extremely powerful feature of SharePoint is the ability to create event receivers for any list type.

An event receiver is a piece of managed code that is launched in response to an event takes place within SharePoint. These events can be triggered in response to changes to list items, creation of new lists items, list items being deleted and much more.

List event handlers are made possible through the concept of event receivers.
An event receiver is a .NET framework class contained in the strong-typed assembly in GAC.

One of the prime examples of using event receivers is to log the entries for admin purpose to track the changes to list data.

Here are the steps to create your own event receivers.

1. Create a class that inherits from SPListEventReceiver or from SPItemEventReceiver.
2. Strong name the assembly.
3. Deploy assembly to GAC
4. Deploy the event receiver with code to SharePoint.

Sample List Item Event Receiver Code:
Using system;
Using system.IO;
Using system.Text;
Using Microsoft.SharePoint;

namespace EventLog
Public class LogEvent : SPItemEventReceiver
Public override void ItemAdded(SPItemEventProperties properties)
WriteTextToLog(String.Format(“[{0}] Item added: {1}”, properties.ListItem.Title,DateTime.Now.Tostring()));
Public override void ItemDeleted(SPItemEventProperties properties)
WriteTextToLog(String.Format(“[{0}] Item deleted: {1}”, properties.ListItem.Title,DateTime.Now.Tostring()));
Public void WriteTextToLog(string text)
StreamWriter file = File.AppendText(@“c\SharePointlog.txt”);
file.write(text + "\n");
Check out for more list/item events at
After you have written your event receivers you can follow steps 2 and 3 mentioned above.
I will explain in my next article how you can deploy the event receiver to SharePoint using code.

No comments:

Post a Comment