| Subcribe via RSS

Accessing field values in a sandbox solution

Oktober 14th, 2010 Posted in Sandbox, SharePoint, Uncategorized

In my case I tried to read a field value of type LinkFieldValue. The field type is defined in the assembly Microsoft.SharePoint.Publishing. I read the field value using this line of code:

var fieldValue = item["MyFieldName"] as LinkFieldValue;

This works pretty fine in a console test application or in a farm solution. In a sandbox solution the following error will occur:

Type ‘Microsoft.SharePoint.Publishing.Fields.LinkFieldValue’ in Assembly ‘Microsoft.SharePoint.Publishing, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c’ is not marked as serializable.

It took me some time to solve this:

string value = item.GetFormattedValue("MyFieldName");
LinkFieldValue fieldValue = new LinkFieldValue(value);

It’s not a beautiful piece of code but it works.

Leave a Reply