Accessing field values in a sandbox solution

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=14.0.0.0, 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.
About these ads

One Response to “Accessing field values in a sandbox solution”

  1. Thx for this hint, i’m developing an sanboxed solution for displaying all item field values… at the moment i only read them with item["MyFieldName"].toString() but with your approach the rsult could be a little bit nicer :)

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 )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: