Sometime its necessary to check some business logic when adding/editing/deleting data from a sharepoint list. In this article I’ll show how to implement the custom logic while adding/editing and deleting data in sharepoint list. For this purpose I have created a Custome List in sharepoint and named it MyNews. This list has following column.
Headline
NewsImage
NewsDetail
ExpiryDate
I will write an event handler for this list which will not allow user to set the expiry date of the MyNews list to be smaller than the Current Date. This will be a very simple event handler but this approach can be used to handle the complex scenarios.
Sharepoint 2007 provide event handlers for two types which are given below:
Asynchronous
Synchronous
ASynchronous events ends with ‘ed’ like ItemAdded, while Synchronous ends with “ing” like ItemAdding. So in case of synchronous events we can cancel a user’s action before it take place. For example we will not allow user to add data in MyNews list, if its expiry date is less than the current date by cancelling the user’s add action. So here we go.
Open Visual studio and create a New class library Project. Add reference of Microsoft.Sharepoint dll in the project. Inherit your class from SPItemEventReceiver
Like this: public class MyEvents : SPItemEventReceiver
Now override the following function: ItemAdding
Like this: public override void ItemAdding(SPItemEventProperties properties)
{
}
Now inside this function I’ll write code which will restricts the user to not add the Items if the Expiry date of the NewsList is < Current date. So here is my code:
DateTime NewsExpiryDate = Convert.ToDateTime(properties.AfterProperties["ExpiryDate"]);
if (NewsExpiryDate != null)
{
if (NewsExpiryDate.CompareTo(DateTime.Now) < 0)
{
properties.ErrorMessage = "Expiry date can not be a Past date";
properties.Cancel = true;
}
}
We have to use properties.AfterProperties[“COLUMN NAME”] to get the value which user has entered, because in case of synchronous events “properties.listItem” will always be null. So you have to check properties.Afterproperties to see that what user has entered.
Then I checked that if the entered expiry date is not a past date. If its past date then I have set appropriate message and set the properties.Cancel = true, which means that this item will not be added in to the List. Now our code is complete. Now you need to give a strong name to your class library project and after that put the dll into the GAC. I have also attached a code of a console application which will be used to register this dll with share point list. You can also use this console application to register your event handler to Sharepoint list. All you have to do is change the assembly and class name.
After doing all this I entered a past date into the list as shown in the following fig:
After entering the past date in expiry date I clicked on Ok button and got the following error:
Download the following MyEvents.cs class for my code:
MyEvents.cs (724.00 bytes)
Download following class for the console application which is used to register the event.
Program.cs (797.00 bytes)