Event receiver, how to show user an error message, without using properties.Cancel and properties.ErrorMessage? RRS feed

  • Question

  • Hi!

    I am using WSS 3.0.

    To display error message from synchronous event receiver we can use this code:

                properties.Cancel = true
                properties.ErrorMessage = string
    .Format("my error message."
    ut my EditForm is customized, so there is a bug and I get "The data source control failed to execute the insert command." error instaead of my error message.

    So I'm trying to redirect to error page with other functions, like:


    SPUtility.TransferToErrorPage("TransferToErrorPage function redirected here!");
    And with SPUtility.Redirect(even getting Context in event receiver):
     MyHandler : SPItemEventReceiver
     HttpContext currentContext;
                currentContext = HttpContext.Current;
     ItemUpdating(SPItemEventProperties properties)
    , SPRedirectFlags.Default, currentContext);
            // ..
    But it does nothing. Handler works and I see aspx page of my list, like nothing happened. No exceptions too.

    How to make redirect correctly and show user an error message, without using properties.Cancel and properties.ErrorMessage?
    • Edited by Rockie_ Saturday, January 23, 2010 2:53 PM deleted waste comments
    Saturday, January 23, 2010 2:53 PM


All replies

  • Hi,
             Set SPRedirectFlags parameter in SPUtility function to "Trusted". So your code becomes

                     public override void ItemUpdating(SPItemEventProperties properties)
                SPUtility.Redirect("",SPRedirectFlags.Trusted, currentContext);

    Milan C>


    • Marked as answer by Rockie_ Monday, February 8, 2010 9:05 AM
    Saturday, January 23, 2010 8:08 PM
  • Redirecting from event receiver is not a good idea.

    1) When you are adding/updating/deleting items from you code - event receivers will be called too.
    2) Same problem with working with SharePoint Web Service.
    3) TimerJobs that call updating/.../... methods of an item - same problem.

    In your case will be better to include redirecting / error showing logic to your form, for example code behind for OK button click.
    Saturday, January 23, 2010 10:52 PM
  • Hi,


         Creating a static HttpContext instance in Event-Handler constructor can be a walk around to achieve error page jumping.

         You might followed solution in the article below:

         HttpContext solution for evenhandler (


         Would you please also try if current.Response.Redirect("Error-page-url") could help you.

         I suggested in this thread ,and it worked for the customer.


         Another solution in my mind is using HttpModule to catch all errors and exceptions, you can add filer logic to make this handler your event handler error.

         Please also see:


         Hope this can help.  


     Best Regards,


    Tuesday, January 26, 2010 3:46 AM
  • Thank You for Your responses!

    Third variant with Milan Chauhan 's remark worked for me:

                                SPUtility.Redirect(properties.WebUrl + @"/ErrorStatusIsClosed.aspx", SPRedirectFlags.Trusted, currentContext);
                            catch (Exception ex)
                                Message(ex.Message + "\n" + ex.Source.ToString() + "\n" + ex.StackTrace.ToString() + "\n");
    It throws an exception that thread is aborted, but it works.
    Monday, February 8, 2010 9:04 AM