Techbubbles

Accessing Web Services from Sandboxed Silverlight application in SharePoint 2010

 

This post discuss about accessing public web services from sandboxed Silverlight application in SharePoint 2010. SharePoint 2010 introduced the concept of sandboxed solution where the application runs in separate process outside the w3p process where SharePoint is running. Sandboxed solutions having the restriction to make a call to external resources like database and web services. This restriction causing the SharePoint developers to access these resources from sandboxed applications. SP2010

The solution is delivering a Silverlight application in sandbox environment which in turn calls the web service. Because Silverlight applications are running from the client machines even though they delivered from the sandbox solution.

Once the application is available in client machine then they can call to the web services. This post shows calling the public web service and binding the results to SharePoint list. It also shows how to interact with SharePoint Client objects.

we are using one of the web service from http://musicbrainz.org/ws/1/release/?limit=10&type=xml&artist={0}. The web service response look like below

image

The strategy is either using Visual Web part or a site page delivered to sandbox to show the web service response. Normally Visual Web parts are not allowed in Sandbox solutions. Visual Studio team created an extension tools called Visual Studio 2010 Share Point power tools. These tools allows you to create web parts in Sandboxed solutions.

image

http://visualstudiogallery.msdn.microsoft.com/8e602a8c-6714-4549-9e95-f3700344b0d9

Option 1:

we have to create a Visual Web Part using the above tools and embed a object tag in web part which will deliver the SilverLight application.

Option 2:  Simply take a site page which allowed in Sandboxed solution then embed object tag which delivers the SilverLight application.

The Visual Studio solution contains two projects one is SharePoint and another one is Silverlight application.

image

The Visual Web Part code will look like below

image

Important point is param having name source which points to .xap file. we are storing this silver light out put in our SharePoint site assets folder. This will deploy the application to client.

In order to make a public web service call from Silverlight application open the silverlight project and write the below code

image

we are using web client class which allows you to make a call to Uris.

image

This is an asynchronous call and when it is completed it calls the below method

image 

The actual parsing logic is inside this OpenReadCompleted method

image

image

we are binding these results to grid in our silver light application.

Now deploy this visual web part to share point sandboxed solution. open the share point web site

image 

image

Now to interact with SharePoint client object model to export the above result to SharePoint list. We can write this logic in Save button click

image

Using client context we can interact with the SharePoint client objects. In order to send this information from client to server you need to make the async call which is ExecuteQueryAsync method.

image

image

At the end we have created a Silverlight application and this application calls the public web service and uses the client object model to write back to SharePoint. In our SharePoint project we have created a Visual Web part that uses the object tag to deliver the Silverlight application to client.


Share this post :


Related Posts:

%d bloggers like this: