Thursday, March 19, 2015

Thread was being aborted in sharepoint custom code


Thread was being aborted is one of the common error in custom code used to build web parts or any other solutions related to sharepoint , lets look into how i resolved it

If you want user to be redirected to new page or site and u are doing it in try catch block above error will be catch always but still user will be redirected to new URL.

ex :

Code : 


 try
            {
                // custom code to perform some action
                HttpContext.Current.Response.Redirect(RedirectURL);
            }
            catch (Exception ex)
            {
                // log the error to event to custom list
            }



Solution 1 : 

so avoid error please move Response.Redirect out of try catch as shown below 
// ***  Code *** //
 try
            {
                // custom code to perform some action
             
            }
            catch (Exception ex)
            {
                // log the error to event to custom list
            }
 HttpContext.Current.Response.Redirect(RedirectURL);

Solution 2 : 

if you cannot avoid using Response.Redirect  out of try catch block then add Response.Redirect with end response parameter set as false   HttpContext.Current.Response.Redirect(RedirectURL,false);



// ***  Code *** //
 try
            {
                // custom code to perform some action
                HttpContext.Current.Response.Redirect(RedirectURL,false);
            }
            catch (Exception ex)
            {
                // log the error to event to custom list
            }



I hope your doubts are clear else comment for further clarification

No comments:

Post a Comment