Pages

Tuesday, September 11, 2012

Get Term Id from Managed Metadata Field in a Sandbox Solution


You are developing a sandbox solution where you want to work with Microsoft.SharePoint.Taxonomy.TaxonomyFieldValue.

You have noticed that you have an exception:

The exception is : System.RunTime.Serialization.SerializationException
Message : Type 'Microsoft.SharePoint.Taxonomy.TaxonomyFieldValue' in Assembly 'Microsoft.SharePoint.Taxonomy, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' is not marked as serializable.
Server stack trace: 
   at System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type)
   at System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context)
   at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo()
   at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter)
   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck)
   at System.Runtime.Remoting.Channels.BinaryServerFormatterSink.SerializeResponse(IServerResponseChannelSinkStack sinkStack, IMessage msg, ITransportHeaders& headers, Stream& stream)
   at System.Runtime.Remoting.Channels.BinaryServerFormatterSink.ProcessMessage(IServerChannelSinkStack sinkStack, IMessage requestMsg, ITransportHeaders requestHeaders, Stream requestStream, IMessage& responseMsg, ITransportHeaders& responseHeaders, Stream& responseStream)


You question: How do I get a value from managed data field?

Microsoft.SharePoint.Taxonomy.dll is not allowed in sandbox solution and that's why you're getting an exception. You can get string value of managed metadata field by calling GetFormattedValue.

But doing so, you will get a value from managed metadata field. 

var relatedNoteFldStaticName = {You metadata column static name}+"0";
item[ relatedNoteFldStaticName  ]

What it does - it pulls the related hidden notes field where Term Id is stored (SPSiteDataQuery in Sandboxed solution with multivalued metadata column)


No comments:

Post a Comment