In this exercise, you add an event receiver to a custom Microsoft SharePoint 2010 list that is triggered when you attempt to delete an item in the list. To complete this task, you must do the following:
In this task, you open the list definition project in Visual Studio 2010.
Aspired to become a SharePoint? Explore the post to discover the know-how on the " SharePoint Training" course
In this task, you create and attach an event receiver to the list definition. This event receiver prevents the deletion of bug items and displays a message.
try{ properties.Cancel = true; properties.ErrorMessage = "Bugs can only be resolved not deleted!";}catch (Exception ex){ return;}finally{ this.EventFiringEnabled = true;}
In this task, you first create an item in the custom list and then trigger the event receiver by trying to delete the item.
Example - 1
Using Visual Studio 2010, choose the new template called "Event Receiver" like so: Choose if you want this to be a Farm Solution or a Sandboxed Solution. Then click next: Finally, choose the Type of receiver you want to create, what event you would like to hook up and to what type. I chose "List Item Events", "Announcements" and "An item is being added":
Click next and let Visual Studio 2010 work its magic. You’re presented with the following project structure that is created for us:
I will not dig deep on how and why the structure of the project looks the way it does now – it will be covered in another article. 1. Adding some basic logic for your Event Receiver In the EventReceiver1.cs file that you’re presented with, you can quite easily add any code you want – and some code has already been added so you don’t have to! The code looks like this out of the box:
Now, what I want to do in order to make sure my event receiver works – is to simply add some dummy-code and have it tested! Add the following code to your ItemAdding-method:
All we do here is check our item that is being added makes a condition to see if the DueDate property is set. The breakpoint is simply added because you easily should see that your code executes and works as expected.
By pressing F5, Visual Studio will take care of the build, packaging, and deployment of your Event Receiver. For more details on the actual F5-experience, I encourage you to read MSDN, SDK and all the blogs out there. You are presented with a web page (VS 2010 launches IE for you as well), where you now can easily test your Event Receiver. In our case, when adding a new Announcement, we should automatically check the DueDate property. Currently, we don’t cancel the event or do anything else – let’s leave that to your imagination.
So – now that we’ve got a very basic Event Receiver in SharePoint 2010, we should add that Custom Error Page we talked about. Start by adding a New Item to your project of type Application Page:
Edit the ErrorDueDate.aspx file to add some rich HTML:
Edit your EventReceiver1.cs file and add the current logic:
Try it out by adding a new Announcement without adding a DueDate: You should now be presented with the following dialog:
As you can see, your custom HTML now appears. Apparently, I didn’t do any fancy design on my Application Page, but you can add more images and whatever else you want to make it more easy for the users to understand what actually went wrong – and how to make it on from there. Example 3:
This is a Step-By-Step example of creating a simple Item added event receiver for a custom list in SharePoint 2010. The event receiver will change the Title of the item to the current Date Time once it's added to the list. We will create and deploy the solution as a sandbox and will run in debug mode. Steps are:
Click Next.
Click finish.
Create List - Create a New Item - Once you add the Item, just refresh the custom list page, and you will see the results.
You liked the article?
Like: 0
Vote for difficulty
Current difficulty (Avg): Medium
TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.