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.
- Synchronous event handler
- 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.
- List-item event handlers
- List event handlers
- Web event handlers
- Workflow event handlers
- 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?
- Inherited from below classes.
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”.
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
- In your event receiver, ItemAdding method creates your validation condition that will return the error.
- 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.
- 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
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";
}
These are facts we need to consider while working with an Event Receiver.
- We should not use Before properties for accessing change of value at the List level.
- Before properties are available only with Document Library.
Below is the context in SharePoint event receiver
- BeforeProperties
- AfterProperties
- properties.ListItem
List:
Library:
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.
List | BeforeProperties | AfterProperties | properties.ListItem |
ItemAdding | No value | New value | Null |
ItemAdded | No value | New value | New value |
ItemUpdating | No value | Changed value | Original value |
ItemUpdated | No value | Changed value | Changed value |
ItemDeleting | No value | No value | Original value |
ItemDeleted | No value | No value | Null |
Library:
Library | BeforeProperties | AfterProperties | properties.ListItem |
ItemAdding | No value | No value | Null |
ItemAdded | No value | No value | New value |
ItemUpdating | Original value | Changed value | Original value |
ItemUpdated | Original value | Changed value | Changed value |
ItemDeleting | No value | No value | Original value |
ItemDeleted | No value | No value | Null |
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