Tuesday, 17 February 2015

Event Receivers in SharePoint

Event Receivers

The event is nothing but action, which will fire something happened within the SharePoint is called event.

There are 2 types of event handlers are there.

  1. Synchronous event handler
  2. Asynchronous event handler

What is the difference between Synchronous and Asynchronous event handler?

  • The synchronous event handler will be fire before the action is performed.
    • Example :- Item Adding, Item Deleting, Item Updating.
  • The asynchronous event handler will be fire after the action is performed.
    • Example: - Item Added, Item Deleted, Item Updated.

In SharePoint 5 types of event, handlers are there.
  1. List-item event handlers
  2. List event handlers
  3. Web event handlers
  4. Workflow event handlers
  5. Email event handlers
    Workflow event handler and Email event handler added in SP-2010 server, these do not exist in SP-2007 server.

Your class inherited from which class in Event Receivers?

What are the abstract methods in event receivers?
  • ItemAdding()
  • ItemUpdating()
  • ItemDeleting()
Which class are you used to get the Properties?
  • SpItemEventProperties class
How do you redirect to custom error page using Event receivers?

When a custom validation error occurs in SharePoint event receiver from your code and you cancel the event, the user will see the error in an ugly default error page!


So in these steps, we will view a custom error page that contains a friendly error, our example is to validate the input data in “Title” column and view an error if the “Title” is not equal “Hello”.

Event Receiver Cancellation Implementation Steps
  1. In your event receiver, ItemAdding method creates your validation condition that will return the error.
  2. we will use “properties” collection if validation condition error true to cancel the event and redirect the user to a custom page and send the error message to this page “see the code in the image below” SharePoint Custom error Event receiver code.
  3. you will find the path to an error page that the event will redirect the user to it for creating a custom error page follow the next part steps
BeforeProperties / AfterProperties in Event Receivers

These are facts we need to consider while working with an Event Receiver.
  1. We should not use Before properties for accessing change of value at the List level.
  2. Before properties are available only with Document Library.
Below is the context in SharePoint event receiver
  • BeforeProperties
  • AfterProperties
  • properties.ListItem
List:
ListBeforePropertiesAfterPropertiesproperties.ListItem
ItemAddingNo valueNew valueNull
ItemAddedNo valueNew valueNew value
ItemUpdatingNo valueChanged valueOriginal value
ItemUpdatedNo valueChanged valueChanged value
ItemDeletingNo valueNo valueOriginal value
ItemDeletedNo valueNo valueNull

Library:
LibraryBeforePropertiesAfterPropertiesproperties.ListItem
ItemAddingNo valueNo valueNull
ItemAddedNo valueNo valueNew value
ItemUpdatingOriginal valueChanged valueOriginal value
ItemUpdatedOriginal valueChanged valueChanged value
ItemDeletingNo valueNo valueOriginal value
ItemDeletedNo valueNo valueNull


Here one more scenario we need to consider before we conclude this topic.

I have one example where I need to prevent the user from entering the same value for a column.

Ex: Suppose column called Description and its initial value is "Test" and user go to the list and update the item for that column with the same value we need to prevent this. How to achieve this?

So if we observe carefully with the above table then we have the answer available immediately.
In the ItemUpdating Event, we can check the condition like


if(properties.ListItem["Description"] != properties.AfterProperties["Description"])
{
         //Cancel the event
         properties.Cancel = true;
         properties.ErrorMessage = "The column is having same original value";
}

No comments:

Post a Comment