Create custom error page for SharePoint event receiver

Standard

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”

SharePoint Custom error Event reciver code

SharePoint Custom error Event receiver code

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”

Add sharepoint mapped layout folder

Add SharePoint mapped layout folder

2-Right click on the Layouts folder then add new application page from “SharePoint” tab in “New” windows “see the image below

Add sharepoint application page

Add SharePoint application page

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”

Custom Error Application Page HTML

SharePoint Custom Error Application Page HTML

SharePoint Custom Error Application Page Load Event Handler

SharePoint Custom Error Application Page Load Event Handler

4-Deploy your visual studio solution and try it , it will looks like that below image

SharePoint event receiver custom error page

SharePoint event receiver custom error page

hope it help you

Advertisements

6 thoughts on “Create custom error page for SharePoint event receiver

  1. You need to properly sanitize the input there. Using your code I was able to inject javascript into the page via the Querystring parameter. You should modify your code behind above to the code below to ensure that the query strings are properly sanitized.

    if (Request.QueryString[“Error”] != null)
    {
    lblMainIssueMessage.Text = HttpUtility.HtmlEncode((string)Request.QueryString[“Error”]);
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s