Note: you can download the full solution by Click Here
When a custom validation error occur in SharePoint event receiver from your code and you cancel the event , the user will see the error in an ugly .Net 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 Cancllation Implementation Steps
1-In your event receiver ItemAdding method create 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 custom page and send the error message to this page “see the code in image below”
3-you will find the path to error page that the event will redirect the user to it for creating a custom error page follow the next part steps
Creating the custom error page
1-From the Visual Studio right click on your project and add new mapped layout folder “see image below”
2-Right click on the Layouts folder then add new application page from “SharePoint” tab in “New” windows “see the image below
3-The application page contains 4 content placeholder tags header , title , body , page title in title area , simply add a label on the body and go to the code behind of it and add code to print the message from the query string “see 2 images below”
4-Deploy your visual studio solution and try it , it will looks like that below image
hope it help you