SharePoint 2010 - Lists and Event Handlers

Referred URL - http://www.c-sharpcorner.com/UploadFile/40e97e/sharepoint-2010-lists-and-event-handlers/

In this article we explore the Event Handling features of a List through code. Using the SPList in Server Object Model we can access event handling for a list.
We can use the event to perform the following activities:

  • Validate the Item
  • Log the information
  • Create associated items

There are multiple Event Types for a List:

  • List Events
  • List Item Events (Add, Edit, Delete)
  • Web Events
  • Feature Events
  • Workflow Events

Create Event Receiver Project
To begin, create an Event Receiver project in Visual Studio.

ShareEvtR1.jpg
You will be prompted for the site:

ShareEvtR2.jpg
By default the machine site will be shown in the dialog box. Leave the default option of the Sandboxed solution and click the Next button.

ShareEvtR3.jpg
Select the List Item Events and use the event source Contacts. Check the check boxes for added, updated and deleted events and click the Finish button.
Note: The site URL can be changed later. For the time being we are using hard-coded URLs.
On clicking the Finish button, the code will be generated for the List Event Receiver and you can place a break-point in the ItemAdding event as shown below.

ShareEvtR4.jpg
Now execute the application and your Event will be added and activated.
Try adding a new contact inside SharePoint:

ShareEvtR5.jpg
On clicking the Save button, the breakpoint inside Visual Studio will be hit:

ShareEvtR6.jpg
SPItemEventProperties
You can cancel the operation by using the Cancel property.
You can report an error message using the ErrorMessage property.
Cancel for Delete
You can cancel an item delete by setting the properties.Cancel = true:
public override void ItemDeleting(SPItemEventProperties properties)
{
       properties.ErrorMessage = "Deleting Item is not permitted!";
       properties.Cancel = true;
}
Try deleting an item from the Contacts inside SharePoint:

ShareEvtR7.jpg
You will get the following message prompted:

ShareEvtR8.jpg
Deploying the Event Receiver
You can take a note that while you stop Visual Studio, the item deletion is allowed inside SharePoint. To make the event receiver permanent, use the project Deploy option.

ShareEvtR9.jpg
Now try Deleting an item without Visual Studio Debugging. You will get the same Error Message Dialog and this concludes the Event Creation and Deployment to a SharePoint site.
Properties of Project
You can change the URL and other properties created through the Wizard. Use Project > Properties to access/modify these properties.

ShareEvtR10.jpg
Features
The event receiver is actually deployed as a feature inside SharePoint. You can view the Feature properties and the associated XML file under Features special folder as shown below.
ShareEvtR11.jpg
The Event code and Elements.xml reside inside the EvntReceiver1 group as shown below.

ShareEvtR12.jpg
References
http://msdn.microsoft.com/en-us/library/bb736146%28v=office.12%29.aspx

You May Also Like

0 comments