Creating custom Web Part properties in Sandbox Solutions

I am currently working on a quick POC for configuring the Drop-down via custom tool pane.  The scope of my POC is to test whether Visual Web Part development can be robustly achieved via Sandbox Solution deployment model.

I started with a web part property to hold the drop-down as follows:

[Personalizable(true)]
public List<DropDownSelection> DropdownListSelections { get; set; }

After attempting to deploy and use the web part, i quickly ran into an error, “Web part property ‘DropdownListSelections’ uses unsupported type <..>, and cannot be run as a sandboxed code web part.”

Allan Dahls’s blog article astutely points out that only a subset of simple types are supported by the SP2010 infrastructure.  Therefore, my workaround is to use a string datatype for the property and rely on json serialization to persist the actual list.


[Personalizabe(true)]
public string DropdownListSelections { get; set; }

public List<DropDownSelection> DeserializeFromProperty()
{
   var serializer = new JavaScriptSerializer();
   var deserializedProperties = (string.IsNullOrEmpty(DropdownListSelections)) ?
                    new List<DropDownSelection>() : serializer.Deserialize<List<DropDownSelection>>(DropdownListSelections);
   return deserializedProperties;
}

public void SerializeToProperty(List<DropDownSelection> _listToSerialize)
{
   var serializer = new JavaScriptSerializer();
   return serializer.Serialize(_listToSerialize);
}

That allowed me to continue using Sandbox Solution deployment model in my POC.

Advertisement