0

Suppose I define a web part property like this:

    private string _myString = "default value";
    [WebBrowsable(true),
    WebDisplayName("My string"),
    WebDescription("A simple bit of text to set"),
    Personalizable(PersonalizationScope.User),
    DefaultValue("My default value"), //this doesn't work for some reason
    Category("My properties")]
    public string MyString 
    { 
        get 
        { 
            return _myString; 
        } 
        set 
        { 
            if(string.IsNullOrEmpty(value))
                throw new Microsoft.SharePoint.WebPartPages.WebPartPageUserException("String must not be empty");

            _myString = value;
        } 
    }

This is how it is explained in various guides and books. The WebPartPageUserException is supposed to cause the validation text to appear in the web part properties pane in the side. For visual web parts, this works like a charm. However, for 'classic' web parts in a sandboxed solution, the page will reload and the web part will display an error about an unhandled exception.

Is this validation inside the properties pane really restricted to visual web parts, or am I missing something? It seems strange how something like this would be impossible in sandboxed solutions.

2 Answers 2

1

It has nothing to do with visual webparts <=> standard web parts. It's purely a sandbox problem.

Thowing exceptions out of sandboxed code isn't supported and will be caught by the sandbox and cost you points.

The code above works great if you just change your solution to being a farm solution, but if you want it in a sandbox you have to move the check into the "rendering" code.

0
0

You should really have a base webpart that the main class inherits from, from the base webpart you can do the following:

    [SPWebCategoryName("give it a name")]
    [DefaultValue("My default value")]
    [Personalizable(PersonalizationScope.Shared)]
    [WebDisplayName("DisplayName goes here")]
    [WebDescription("A simple bit of text to set")]
    [WebBrowsable(true)]

or in your case you can do this:

[
  System.Web.UI.WebControls.WebParts.WebBrowsable(true),
  System.Web.UI.WebControls.WebParts.Personalizable(PersonalizationScope.User),
  System.Web.UI.WebControls.WebParts.WebDescription("Description goes here"),
  Microsoft.SharePoint.WebPartPages.SPWebCategoryName("Custom Properties"),
  System.Web.UI.WebControls.WebParts.WebDisplayName("DisplayName goes here")]

also what exception was you getting? did you debug and put a break point on your get set method and looked at your uls or eventviewer to see the full error?

hope this helps :)

1
  • It is the WebPartPageUserException that I throw (according to ULSViewer). It appears that for visual web parts it will catch that exception and show validation text in the web part property pane, while standard sandboxed web parts do not catch that exception and this causes the web part to fail. Of course I could catch the exception myself but What I really want is to validate INSIDE the web part property pane. Oct 5, 2012 at 8:42

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.