none
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.

    1)
    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:

    2)

    SPUtility.TransferToErrorPage("TransferToErrorPage function redirected here!");
    And with SPUtility.Redirect(even getting Context in event receiver):
    3)
        public
     class
     MyHandler : SPItemEventReceiver
        {
            public
     HttpContext currentContext;
           
            public
     MyHandler()
            {
                currentContext = HttpContext.Current;
            }
            //..
    
           
            public
     override
     void
     ItemUpdating(SPItemEventProperties properties)
            {
                SPUtility.Redirect("http://microsoft.com"
    , 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

Answers

All replies

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

                     public override void ItemUpdating(SPItemEventProperties properties)
            {
                SPUtility.Redirect("http://microsoft.com",SPRedirectFlags.Trusted, currentContext);
            }
     

    Regards,
    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 (http://www.sharepointkings.com/2008/05/httpcontext-in-eventhandler.html)

     

         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.

         http://social.technet.microsoft.com/Forums/en-US/sharepointdevelopment/thread/4aad64a6-ee5c-4115-b189-d87b16077352

        

         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:

         http://blogs.msdn.com/jannemattila/archive/2008/02/04/catching-unhandled-exceptions-in-sharepoint.aspx

     

         Hope this can help.  

                                                                                                  

     Best Regards,

    -Aaron

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

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

                            try
                            {
                                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