Thursday, January 04, 2007

List Events in Windows SharePoint Services 3.0

Ever had these questions while working with lists/document library:
  • Is it possible to restrict users from entering invalid data?
  • Is it possible to have counters for files created ,deleted?
  • Is it possible to display users with "Custom Error" in case they try entering invalid data?
All these questions were kind of un-answered during days of SharePoint 2003 ,fortunately days have changed and Microsoft has solutions for it .Its the MOSS 2007/WSS 3.0 Events.

In WSS 3.0 events that are triggered by list allow you to hook up with code when the list itsms are added,changed or removed.
Events are divided in 2 category :
  • Events that fire before an action occurs :This allow you to perform custom validation or processing of data that is about to be added to the list,modified or deleted.
  • Events that fire after a certain action occurs
In WSS 3.0 it is possible to communicate responses back to the user interface.

You can create an event handler assembly using Visual Studio .NET by creating a class inhering from SPItemEventReceiver base class present in Microsoft.SharePoint.dll assembly.

using System;
using Microsoft.SharePoint;

namespace WSS.FirstTest
{
public class MyFirstEventhandler:SPItemEventHandler
{
}
}


This class will be instantiated by the WSS 3.0 when action occurs within the list. In order to define custom behaviour of these events we need to override the methods in SPItemEventReceiver class. Following method can be overriden:
  • ItemAdding
  • ItemAdded
  • ItemUpdating
  • ItemUpdated
  • ItemDeleting
  • ItemDeleted
  • ItemCheckingIn
  • ItemCheckedIn
  • ItemCheckingOut
  • ItemCheckedOut
  • ItemUncheckingOut
  • ItemUncheckedOut
  • ItemFileRenaming
  • ItemFileRenamed
  • ItemAttachmentAdding
  • ItemAttachmentAdded
  • ItemAttachmentDeleting
  • ItemAttachmentDeleted
We will override method ItemDeleting as sample.

public override void ItemDeleting(SPItemEventProperties property)
{
if(property.BeforeProperties["Amount"].ToString() >"500")
{
property.Cancel=true;
property.ErrorMessage ="Any amout greater than 500 cannot be deleted" ;
}
}

In this case when user tries to delete any item whose Amount is greater than 500 gets an error message .

Now as we have done with the Event assembly the next task is to register the assembly.In my next Blog we can check out the registering of events to a list.

3 comments:

Anonymous said...

We found out the hard way that when an anonymous user interacts with a list (i.e. adds an item), the event handlers are not fired. This is a bug in SharePoint 2007 (acknowledged by MS) that will "not be fixed any time soon". So, for Internet facing sites where an anonymous user completes a form that inserts a new entry in a list, the ItemAdded() event will not fire.

shines said...

Is it possible to capture the Item downloaded properties?

Naresh said...

is it possible to give error message with in the same page instead of displaying a stnadrad sharepoint error page with no flexible navigation to get back to the library.

Thanks,
Naresh