1

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?

2 Answers 2

3

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));
        }
    }
    
4
  • can you describe the 1st?
    – Steve B
    Commented Sep 27, 2012 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. Commented Sep 27, 2012 at 13:11
  • Answer has been expanded slightly Commented Sep 27, 2012 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 ;-) Commented Sep 27, 2012 at 13:45
1

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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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