Creating Event Receivers
Now that you understand how event receivers let us implement custom business logic for our SharePoint applications, let’s take a look at how we can create them and hook them up with the platform.
The options in the What Type Of Event Receiver Do You Want? drop-down list correspond to the receiver base classes discussed earlier. The exception to this is the List Email Events option, which we’ll look at later in this chapter. The items in the What Item Should Be The Event Source? drop-down list correspond to the event host that will raise the event. Each option represents a specific instance of an event host.
From this code snippet, we can see that our event receiver is derived fromthe SPItemEventReceiver base class, and both of the defined methods accept SPItemEventProperties objects as parameters.
By creating a new item with the title set to Update Me, the event handler updates the title value before the item is stored in the content database. Setting the title to Throw Error or Redirect illustrates the capability to return a custom error message to the user and to redirect to a custom page. In both of these cases, the item is not added to the list.
Note the addition of the Synchronization element for the List Event Receiver Item Added receiver.
So that we can confirm that our event receiver is being called, update the code in the ItemAdded method like so: public override
Enabling or Disabling Event Firing
You"ve seen how to create event handlers using the Share PointCustomization Wizard in Visual Studio. Let’s look at how we can handle additional events in an existing event receiver.
As we did earlier, we can now add whatever custom code we need to these method stubs.
After deploying and repeating the test, we can see that our code now behaves as expected: it updates the item title to Updated Title0.
Fundamentally, you can bind event receivers to event hosts in two ways. The first method, which is used by the SharePoint Customization Wizard in Visual Studio, is via CollaborativeApplication Markup Language (CAML). When an event receiver is added to a SharePoint project, two files are added to the solution: An Elements.xml file and a code file to contain the implementation of the event receiver. The Elements.xml file contains XML similar to this snippet:
Element files and CAML are covered in more detail in Chapter . For the purposes of binding event receivers, the key thing that you need to know is that each Receivers element can contain one or more Receiver elements, where a Receiver element defines an event receiver class.
The second method, which offers a much greater degree of granularity, is to bind event receivers programmatically. While the CAML method is definitely the easiest to use, it has a major drawback in that the lowest level of granularity is the list template. To bind an event receiver to a particular list would require a new list template specific to that list. In our example, we bound our event receivers to the custom list template (ListTemplateID 100).
In our simple test site, only one list used this template, so this didn’t present a problem.
Let’s see what happens if we add another custom list.
When clicking the Save button, an error page is shown. From this, we can see that our new list is also calling the event handlers that we configured for our test list. To resolve this problem, we need to programmatically attach our event receiver to our test list only. Take the following steps to make this change:
One other type of event receiver base class that we haven’t looked at is the SPEmailEventReceiver. This receiver can be used to handle the Email Received event for a particular document library or list. The first thing to note about e-mail events is that not all lists and libraries can accept incoming e-mail, and those that can accept it include document libraries, announcement lists, calendars, and discussion lists. Also, before e-mail can be sent to a list or library, it must first be configured in Central Administration and within an organization’s network. This is a relatively in-depth process;
Share Point 2010 Related Interview Questions
|Web Services Interview Questions||XML Interview Questions|
|Share Point 2010 Interview Questions||ASP.NET Interview Questions|
|Share Point Administration Interview Questions||BizTalk Admin Interview Questions|
|Microsoft Office SharePoint Server (MOSS) Interview Questions||Biztalk Server Interview Questions|
|Asp Dot Net Mvc 4 Interview Questions||Biztalk Esb Toolkit Interview Questions|
|InfoPath Interview Questions|
Share Point 2010 Tutorial
The Microsoft Sharepoint 2010 Platform
Developing With Sharepoint 2010
Presentation Layer Overview
Client Object Model
Infopath Forms Services
Enterprise Content Management
User Interface Customization
Application Services Overview
Service Application Framework
Word Automation Services
Data Access Overview
Linq To Sharepoint And Spmetal
Business Connectivity Services
User Profiles And Social Data
Packaging And Deployment Model
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.