Export (0) Print
Expand All
Expand Minimize

Calling Public Web Services from a Sandboxed Silverlight Application

SharePoint 2010

SharePoint Visual How To

Summary:  Learn how to create sandboxed Microsoft Silverlight solutions that can call public web services in Microsoft SharePoint 2010.

Last modified: September 12, 2012

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

Overview

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.

Code It

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.

<object data="data:application/x-silverlight-2,"
        type="application/x-silverlight-2"
        width="100%" height="100%">
    <param name="source"
     value="[Path to Silverlight XAP file]" />
</object>


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.

WebClient client;
List<string> albums;

public MainPage()
{
    InitializeComponent();

    // Initialize web client.
    client = new WebClient();
    client.OpenReadCompleted += 
      new OpenReadCompletedEventHandler(
        client_OpenReadCompleted);
}

private void Search_Click(
  object sender, RoutedEventArgs e)
{

    // Call public web service.
    string requestUri =
    "http://musicbrainz.org/ws/1/release/?limit=10&type=xml&artist={0}";
    client.OpenReadAsync(
      new Uri(String.Format(
      requestUri, Keyword.Text)));
}

void client_OpenReadCompleted(
  object sender, OpenReadCompletedEventArgs e)
{

    // Process returned data.
    XElement results;
    albums = new List<string>();

    if (e.Error != null)
      {return;}
    else
    {
      XNamespace ns =
      @"http://musicbrainz.org/ns/mmd-1.0#";
      results = XElement.Load(e.Result);

      var q = from r in results.Descendants(ns + "release")
              select new
              {Title = r.Element(ns + "title").Value};

      foreach (var i in q)
      {
        albums.Add(i.Title);
      }

    }
}

Using the Client Object Model

In addition to accessing a public web service, the solution might also have to access objects within the SharePoint site. The Silverlight application can access the objects within the SharePoint site by using the client object model. The client object model enables client applications that are written in managed code, Silverlight, or ECMAScript (JavaScript, JScript) to access SharePoint objects by using syntax similar to that used in the server-side SharePoint object model. The following code shows how to take the data that is returned from the public web service call and write it into a SharePoint list named MusicBrainz.

ClientContext ctx = new ClientContext({SiteUrl});
List list =
         ctx.Web.Lists.GetByTitle("MusicBrainz");
ctx.Load(list);

// Add list items.
foreach (string album in albums)
{
  ListItemCreationInformation listItemCI =
    new ListItemCreationInformation();
  item["Title"] = album;
  item.Update();
}

ctx.ExecuteQueryAsync(
  succeedListener, failListener);

Read It

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.

See It

Watch the video

Watch video

Length: 00:12:15

Click to grab code

Grab the code

Explore It

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 scot@shillier.com.

Show:
© 2014 Microsoft