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

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.

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

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.

share|improve this answer
    
Oh I see. Thanks for clearing that up! –  Spongeroberto Squarepantalones Oct 8 '12 at 8:20
add comment

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

share|improve this answer
    
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. –  Spongeroberto Squarepantalones Oct 5 '12 at 8:42
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.