Calling Public Web Services from a Sandboxed Silverlight Application
Summary: Learn how to create sandboxed Microsoft Silverlight solutions that can call public web services in Microsoft SharePoint 2010.
Applies to: SharePoint Foundation 2010 | SharePoint Server 2010 | Visual Studio | Visual Studio 2008 | Visual Studio 2010
Provided by: Scot Hillier, Scot Hillier Technical Solutions, LLC | About the Author
Microsoft SharePoint 2010 introduces the concept of the sandboxed solution. Sandboxed solutions run in a separate process outside the w3wp.exe process where SharePoint executes. Isolating a sandboxed solution promotes farm stability by preventing poorly performing solutions from affecting the SharePoint farm. As part of the isolation strategy, sandboxed solutions cannot make calls to external resources, such as databases or web services. This restriction can be problematic for SharePoint developers whose solutions need access to external resources. One approach to solve this problem is to use a Microsoft Silverlight application that is delivered from a sandboxed solution to call a web service.
The key to developing a sandboxed Silverlight application is to create a Web Part or site page that can be deployed as a sandboxed solution. The Web Part or site page can then use an object tag to reference a Silverlight application. The application is downloaded to the client, where it can access public web services.
Delivering the Silverlight Application
You create a sandboxed solution in Microsoft Visual Studio 2010 by setting the sandboxed solution property of the SharePoint project to true. After you create a sandboxed solution in Visual Studio 2010, you can add a Web Part or site page to the project. Although Visual Web Parts are not usually supported in sandboxed solutions, the Visual Studio team has created an extension to the SharePoint 2010 tool set that enables you to create Visual Web Parts in sandboxed solutions. The extension is available in the Visual Studio Gallery. Whether you decide to use a Web Part or a site page to deliver the Silverlight application, you must create an object tag similar to the following code.
Accessing the Public Web Service
Because Silverlight applications are delivered from the sandbox to the client computer, they can access public web services even though sandboxed solutions are usually prevented from doing so. The WebClient class is used in Silverlight to call public web services. The call is made asynchronously with a callback function that fires when the data is returned from the service. The following code shows how to call the public music service MusicBrainz by using the WebClient class.
Using the Client Object Model
Sandboxed solutions contribute significantly to the overall stability of SharePoint 2010 farms. However, the stability comes at the cost of being unable to access external resources from inside the sandbox. Silverlight applications offer an elegant solution to this challenge because they run on the client computer. This enables the solution to be deployed to the sandbox, but delivered to the client. Because the solution runs on the client computer, it can access public web services. Additionally, the solution can use the client object model to interact with SharePoint objects.
> [!VIDEO https://www.microsoft.com/en-us/videoplayer/embed/5d5189da-edb8-4969-8f07-a8718f1f606e]
About the Author
Scot Hillier is an independent consultant and Microsoft SharePoint Most Valuable Professional (MVP) focused on creating solutions for Information Workers with SharePoint, Office, and related .NET Framework technologies. He is the author/coauthor of 15 books and DVDs on Microsoft technologies, including Inside Microsoft SharePoint 2010 and Professional Business Connectivity Services. Scot splits his time between consulting on SharePoint projects, speaking at SharePoint events like Tech-Ed, and delivering training for SharePoint developers. Scot is a former U.S. Navy submarine officer and graduate of the Virginia Military Institute. Scot can be reached at firstname.lastname@example.org.