Sharepoint 2007 supports various events on Lists. These events can be broadly classified into two categories - Synchronous and Asynchronous.
Synchronous events such as "ItemAdding" or "ItemUpdating" occur before the action has taken place. They occur synchronously, in other words, these are in the singular pipeline of events that must finish in order for the action to take place. This means, you can effectively cancel the occurring event, should you choose to do so.
Asynchronous events on the other hand, such as ItemAdded or ItemUpdated occur after the action has taken place. For instance, you may want to scrub the data after it has been entered, or you may want to email the administrator if a field has been added - as an FYI. These do not affect the pipeline of events.
It is important to note that there are events at numerous levels - at the item level, at the list level, site level and so on so forth.
So the obvious question is, "How can you tap into such events?"
Well, I am going to demonstrate that by creating a survey on my sharepoint site, and then rigging that survey LOL. :).
So go ahead and click on Site Actions -> Create
Then go ahead and create a new Survey as shown below:
Then go ahead and set up a survey, with one question -
"Is Sahil a good boy?"
With two possible answers "Yes"/"No" presented as a radio button choice to the user.
Now obviously, Sahil is a good boy, so you wouldn't want anyone saying anything else right? SHWEET!! So next I am going to go ahead and rig this survey so the user cannot answer "No", the only right answer is "yes".
To do so, I will need to tap into the ItemAdding event, and to do that, I will need to inherit from the SPItemEventReceiver and create my custom event receiver, and then override the ItemAdding method. Once I do that, I could then validate the data as necessary. The code can be seen below:
public class SahilIsGood : SPItemEventReceiver
public override void ItemAdding(SPItemEventProperties properties)
SPList SahilSurvey = properties.OpenWeb().GetList("http://homepc/Lists/Sahil");
string currentResponse =
SahilSurvey.Items[SahilSurvey.Items.Count - 1]["Is Sahil a good boy?"].ToString();
if (currentResponse != "Yes")
properties.ErrorMessage = "The only valid response is YES you nitwit!!";
properties.Cancel = true;
As you can see in the code above, I first get the appropriate survey (which is a list), and then I get a hold of the last Item added. And if the user said "No", I respond with a polite message saying "The only valid response is YES you nitwit!!" and effectively cancel his input.
Now since this is an SPItemEventReceiver, it will need to be placed in the GAC, so go ahead and strong name it and put it in the GAC.
GREAT!! Your Event receiver is setup, but you still can't use it. Why? Because you haven't bound it to your list yet. So lets do that next. You can do so by using a feature, or as I'm going to do it, using a simple Console Application. Here's the code:
SPSite site = new SPSite("http://homepc") ;
SPWeb web = site.OpenWeb();
SPList survey = site.Lists["Sahil"];
string assemblyName = "ListReceiver, Version=188.8.131.52, Culture=neutral, PublicKeyToken=dd4d54c05b75eae9";
string className = "ListReceiver.SahilIsGood";
survey.EventReceivers.Add(SPEventReceiverType.ItemAdding, assemblyName, className);
Go ahead and run this, this will bind the Event Receiver to the Sahil survey list. Here is a helpful tip: If you're in the midst of debugging, you will have to do IISRESET frequently. This is because IIS will load the assembly from the GAC and won't pick up your changes.
Okay, now go ahead and take the survey. Just for kicks, I am going to click on "No" as my answer, which as I already mentioned is a thoroughly senseless and incorrect answer as shown below:
Now when you go ahead and click on "Finish Survey", Sharepoint is now smart enough to prompt you with the following message:
NICE !! :).
Again, this is something you couldn't do in Sharepoint 2003.
Thou shalt -