• USA : +1 973 910 5725
  • INDIA: +91 905 291 3388
  • info@tekslate.com
  • Login

Add Event Receivers to SharePoint 2010 Lists and Examples

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:

Open the List Definition Project

In this task, you open the list definition project in Visual Studio 2010.

To open the list definition project

  1. Start Visual Studio 2010
  2. On theFile menu, point to Open, and then click Project/Solution.
  3. Navigate to the list definition project file (sln) and then clickOpen.

Add the Event Receiver Code

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.

To add the event receiver to the list definition

  • In Solution Explorer, right-click theBugs node, point to Add, and then click New Item.
  • In theNew Project dialog window, in the Installed Templates section, click Visual C#, click SharePoint, and then click 2010.
  • From the project items, clickEvent Receiver.
  • TypeBugListItemEvent in the Name box, and then click Add.
  • In the SharePoint Customization Wizard, select theAn item is being deleted option, and then click Finish.
  • In Solution Explorer, expandBugListItemEvent, and then open the xml file.
  • To make sure that the event receiver only binds to the custom list, ensure theListTemplateId attribute in theReceivers element is set to 10001.
  • In Solution Explorer, expandBugListItemEvent and then open cs. You should see the overridden ItemDeleting method.
  • Insert the following code into the body ofItemDeleting method after the ItemDeleting(properties);statement. This code prevents the deletion of an item and display an error message.
  • In Solution Explorer, right-click theBugs node and then click Deploy. If you get a deployment error message, click Resolve Automatically.
  • Open the website you specified previously.
  • On the Home page, in the left navigation pane, click theBugs
  • On theList Tools tab, click Items, and then on the New Item drop-down list, click Bug Item. The Bugs – New Item screen is displayed.
  • To create a bug, type information into the applicable boxes, and then clickSave.
  • Next, try to delete the item. You should receive a the custom error message.

try{    properties.Cancel = true;   properties.ErrorMessage = “Bugs can only be resolved not deleted!”;}catch (Exception ex){   return;}finally{   this.EventFiringEnabled = true;}

Test the Solution

In this task, you first create an item in the custom list and then trigger the event receiver by trying to delete the item.

To test the solution

  1. Open the website you specified in the list definition project.
  2. On the Home page, in the left navigation pane, click theBugs
  3. On theList Tools tab, click Items, and then on the New Item drop-down list, click Bug Item. The Bugs – New Item screen is displayed.
  4. To create a bug, type information into the applicable boxes, and then clickSave.
  5. Next, try to delete the item. You should receive a message similar to the one shown in Figure 1.

 

Example – 1 

Creating an Event Receiver in SharePoint 2010 with a Custom Error Page

Using Visual Studio 2010, choose the new template called “Event Receiver” like so:

4

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“:

 

5

Click next and let Visual Studio 2010 work it’s magic.You’re presented with the following project structure that is created for us:

Aspired to become an SharePoint? Explore the post to discover the know-hows on SharePoint Training Course.

6

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:

7

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:

8

 

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.

2. Press the Holy Button (F5)

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.

Cancelling an Event in SharePoint 2010

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:

9

  • Visual Studio 2010 will now automatically add a mapped folder called “Layouts” which is mapping to the _layouts folder in the SharePoint Root (14-folder):

10

Edit the ErrorDueDate.aspx file to add some rich HTML:

11

  • Note that I am referring to an image as well. I just popped that into the same Layouts-folder that was created for my Event Receiver project.

Edit your EventReceiver1.cs file and add the current logic:

12

  • This will now cancel the event if theDueDate field is empty, and show the user a custom error page that you designed yourself.
  • Hit F5 and let Visual Studio 2010 work it’s magic again

Try it out by adding a new Announcement without adding a DueDate:

13

You should now be presented with the following dialog:

14

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:

Creating Event receiver in SharePoint 2010

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 current Date Time once its added to the list. We will create and deploy the solution as a sandbox and will run in debug mode.

Steps are:

  • Open Visual Studio 2010 and create a new Project.
  • In project types select Event Receiver and give it a name (Select .net framework 3.5).
  • Next, In the SharePoint Customization Wizard, choose Deploy as a sandboxed solution and specify the site url where you want the event handler to be deployed. See the Screen below.

Click Next.

  • Next, in Event Receiver Settings dialog select the “List Item Events” as a type of event receiver and “Custom List” in the Item source drop down. Select “An Item was Added” from Handle the following events and click next. See the screen below.
  • 15

 

Click finish.

  • Next, We will write some quick code to Change the Title of an item to the current Data and Time when it is added to a custom list.public override void ItemAdded(SPItemEventProperties properties)
    {
    base.ItemAdded(properties);
    SPListItem _currentItem = properties.ListItem;
    _currentItem[“Title”] = DateTime.Now.ToString();
    _currentItem.Update();
    }
  • We are now all set to deploy our event receiver to the site we specified in Step 3. To build and deploy just press “F5″.
  •  Once press F5 the solution gets deployed to your site and your Visual studio project goes into a debug mode. You can navigate to SPSite2010{site url}->Site Actions->Site Settings -> Under Galleries -> solution and verify that your solution is now deployed to the site. See the screen below.

16

  1. Now we will quickly create a new custom List “My Custom List” and add a new item to test our handler.

Create List – Create a New Item –

Once you add the Item just refresh the custom list page and you will see the results.

17

For Indepth understanding of SharePoint click on

 

 

Summary
Review Date
Reviewed Item
Add Event Receivers to SharePoint 2010 Lists and Examples
Author Rating
5

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on Add Event Receivers to SharePoint 2010 Lists and Examples"

Leave a Message

Your email address will not be published. Required fields are marked *

Site Disclaimer, Copyright © 2016 - All Rights Reserved.