Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top