SPWeb.Properties serialization in the Sandbox

I was trying to store and retrieve some properties on an SPWeb object inside the sandbox. SPWeb has SetProperty() and GetProperty() methods, which accept and retrieve objects. I thought I’d use an enumeration to represent my value, and I’d cast my enumeration value when I retrieve it. However, when I tried this I got an exception of the form:

System.Runtime.Serialization.SerializationException was unhandled Message=Unable to find assembly ‘<my assembly strong name>’

Interesting. I tried similar code within a Console application using the full SharePoint API…

    public enum Color
    {
        Red,
        Green,
        Blue
    }
    class Program
    {
        static void Main(string[] args)
        {
            string url = "http://sp/";
            using (SPSite site = new SPSite(url))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    web.SetProperty("TempKey", Color.Green);
                    Color c = (Color) web.GetProperty("TempKey");
                    Console.WriteLine(c);
                    Console.ReadLine();
                }
            }
        }
    }

This worked fine, so it seemed to be something to do with the Sandbox. However, all the bits of the API I was using were allowed in the Sandbox. Eventually, I found the solution – and it turns out the problems is with Serialization inside Sandbox solutions. From Florin Muntean’s post:

The problem is that the serialization mechanism inside .NET requires to load the assembly for reflection. The SandBox assembly can’t be found because it is not saved in the GAC or any of the SharePoint folders as it only resides in memory and on the Content Database.

A work around for this to create your own serialization to xml/string using custom methods that will save/restore your data using one of the .NET classes (that can be serialized because they live in GAC).

Actually, it’s pretty obvious when someone explains it to you!

Following that advice, instead of storing the enumeration I stored the the .ToString() of it’s value, and parsed it back to the enum when I retrieved it:

public static LogLevel Level
{
    get
    {
		SPWeb rootWeb = SPContext.Current.Site.RootWeb;
		object obj = rootWeb.GetProperty(LoggingConsts.LOGLEVEL_PROPERTY);

		LogLevel lvl = LogLevel.None;
		if( obj != null ) {
			lvl = (LogLevel) Enum.Parse(typeof(LogLevel), obj.ToString());
		}
		return lvl;
    }
    set
    {
		SPWeb rootWeb = SPContext.Current.Site.RootWeb;
		rootWeb.SetProperty(LoggingConsts.LOGLEVEL_PROPERTY, value.ToString());
		rootWeb.Update();
    }
}

And it worked like a charm. Nice one Florin!

Advertisements
SPWeb.Properties serialization in the Sandbox

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s