Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

The title says it all, when there's a error in my webpart I can't load the whole page.

I'd like just to don't display the webpart and still see the rest of the page and not just get the error page. Can this be done without catching all errors?

share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted

You have two options:

  1. Catch all errors in your web part and then log them to ULS, show in hidden div, ...
  2. Develop you web part in a Sandboxed solution, then SharePoint will take care of protecting the rest of the page

To elaborate on the first. It can be implemented using this pattern:

  • Add a string variable errorMessage

    string errorMessage;
    
  • Write each overloaded "event" function to exit if previous error and catch any errors

    protected override void CreateChildControls()
    {
        try
        {
            if (errorMessage != null)
                return;
            // real code
        }
        catch (Exception ex)
        {
            // Log to ULS
            // ...
            // Set Error Message
            errorMessage = "Error in CreateChildControls";
        }
    }
    
  • Override RenderContent to render error (and only error) if it occurred

    protected override void Render(HtmlTextWriter writer)
    {
        if (errorMessage == null)
        {
            try
            {
                var textWriter = new StringWriter();
                using (var tempWriter = new HtmlTextWriter(textWriter))
                    base.Render(tempWriter);
                writer.Write(textWriter.ToString());
            }
            catch (Exception ex)
            {
                // Log to ULS
                // ...
                // Set Error Message
                errorMessage = "Error in CreateChildControls";
            }
        }
        if (errorMessage != null)
        {
            writer.Write("<span class='error'>An unexpected Error occurred. Contact the fool who created this web part</span><div style='display:none;'>{0}</div>",
                HttpUtility.HtmlEncode(errorMessage));
        }
    }
    
share|improve this answer
    
can you describe the 1st? –  Steve B Sep 27 '12 at 12:43
    
Sandboxed solution looks like what I was looking for. I just had the option to develop as a farm solution (sandbox was grayed out). I'll check out what's the problem and try this. –  Aljoscha Husp Sep 27 '12 at 13:11
    
Answer has been expanded slightly –  Per Jakobsen Sep 27 '12 at 13:43
    
Note that using sandboxed solutions you'll be very limited in what you can do. And they are deprecated in SP2013, but at that time we're all writing apps with App Parts instead of Web Parts ;-) –  Per Jakobsen Sep 27 '12 at 13:45
add comment

If you want to just remove the Web Part (or disable them all) you can add

?contents=1

to your querystring, then you can manage your Web Parts

share|improve this answer
    
Sounds helpful, thanks for your answer! –  Aljoscha Husp Sep 27 '12 at 13:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.