Silverlight and SharePoint User Profile Service GUIDs

Way back before the 2009 SharePoint Conference I was building a Silverlight and SharePoint demo and ran into an issue whenever I tried to use Silverlight and call certain methods of the SharePoint User Profile Service. If the web service returned a GUID type, which is part of the PropertyData type from methods like GetUserProfileByName, I would get the error:

The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://microsoft.com/webservices/SharePointPortalServer/UserProfileService:GetUserProfileByNameResponse. The InnerException message was ‘Error in line 1 position 528. Element ‘http://microsoft.com/webservices/SharePointPortalServer/UserProfileService:Value' contains data of the ‘http://microsoft.com/wsdl/types/:guid' data contract. The deserializer has no knowledge of any type that maps to this contract. Add the type corresponding to ‘guid’ to the list of known types – for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer.’. Please see InnerException for more details.

The inner exception is:

“Error in line 1 position 528. Element ‘http://microsoft.com/webservices/SharePointPortalServer/UserProfileService:Value' contains data of the ‘http://microsoft.com/wsdl/types/:guid' data contract. The deserializer has no knowledge of any type that maps to this contract. Add the type corresponding to ‘guid’ to the list of known types – for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer.”

According to Yavor Georgiev (@digthepony) in his post: Workaround for accessing some ASMX services from Silverlight 4

“The error is causes by the fact that ASMX has a slightly different schema for the Guid and char types, which is currently not supported in Silverlight. We are looking at addressing this as soon as possible.”

So, it is going to get fixed and he has a great workaround for Silverlight 4 using Endpoint Behaviors. A simple, elegant solution that inspects the massage before it is parsed and “adjusts the way char and GUID types are represented”. Sweet!

Silverlight 3 and Windows Phone

But, what about Silverlight 3 and Windows Phone? Neither version has the goodness of Endpoint Behaviors. After digging deep into the differences, Yavor and Carlos Figueira figured out the deal and another elegant workaround that solves the problem on both platforms. The process involves creating a “fake” IClientMessageInspector, Yavor mentions the approach in this post: Accessing SharePoint UserProfileService from Windows Phone 7. So with his sample files in hand you just need to create an IClientMessageInspector that will fix the message in the AfterReceiveReply method. This is what the final version of class looks like:

How do I use it?

The real beauty of this solution is how easy it is to add to an existing project. Ordinarily you would add a Service Reference to the User Profile Service and then create a BasicHttpBinding and EndpointAddress and pass them to the constructor of the UserProfileServiceSoapClient like this:

Of course this would end in our now famous exception. The right approach is to make use of the fake message inspector.

How cool is that? You can easily incorporate this solution into your projects. You can download a simple project I created to demonstrate the differences.

Download the sample project: SilverlightGuidAsmx.zip.

|| Development || People Who Rock || SharePoint 2007 || SharePoint 2010 || Silverlight || Windows Phone

comments powered by Disqus

Let's Get In Touch!


Ready to start your next project with us? That’s great! Give us a call or send us an email and we will get back to you as soon as possible!

+1.512.539.0322