Monday, March 8, 2010

As part of the introduction series, I want to present the advantage of the client object model introduced in SharePoint 2010. There are great advantages with this model as it don't require SharePoint needs to be installed on the client machine. We just need to refer the client dlls which Microsoft SharePoint provides and based on them we will write code to communicate with SharePoint server. In this article we will go through Silverlight Client Object Model. If you want to know the other client object model types go here. ECMAScript and Managed client object models.

To communicate with the SharePoint server in Silverlight context we need to give two client SharePoint DLL references to the silver light project.DLL's Needed:Microsoft.SharePoint.Client.Silverlight.dll and Microsoft.SharePoint.Client.Silverlight.Runtime.dll. They can be found at "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\ClientBin".
OK, we understand the concept and we will create a project and implement code for better understanding on how it works.

Give some name to the project. In my example, I have given some meaningful name like "SP2010Silverlight_HelloWorld" and create the project.

Now, you see the below screen.

What this meaning is "Do you want to create an ASP.NET web site and host the XAP file generated to the web site". For our example, it's really not needed. But, there is no problem by using that.

Now next step is getting the SharePoint Silverlight Client Dll's reference to our project. So, for this get the SharePoint dll's to the client machine [Where we created project] and paste the DLL's in some safe location. I copied them to C:\SP2010_ClientDLL\.

Now, go to Visual Studio 2010 project right click on project -> select References and browse to location where client dll's copied and select both dll's and hit ok as shown in below figure.

After you added all references the references folder should look like this.

Now we are ready with all prerequisites and part left is with writing code. I will show you simple code on how to write the code for getting web site title and description using Silverlight Client OM.

Before start coding, we need to add reference to the namespace in page by declaring using keyword as shown below.

using Microsoft.SharePoint.Client;

This is the code to get data from a SharePoint server, in this example we are retrieving Title and Description of a web site.

Place all the code above given in the both files of your project. Note: Remember to change the web url given in the code "http://nb16" to actual SharePoint server url.

Till now, what we have done is, writing and complete code for loading the site data. But, we need to understand the above code.
In the above code, there is a callback function used. Which is asynchronous and loads data. But, you may confuse at the line delegate UpdateUIMethod(). I copied the below text from MSDN to better understand about the delegate and why it's needed.

"Because query execution is asynchronous when you use the SharePoint Foundation Silverlight object model, you must pass delegates for callback methods as parameters in the ExecuteQueryAsync(ClientRequestSucceededEventHandler, ClientRequestFailedEventHandler) method, similarly to query execution in the ECMAScript object model. However, to run code that makes changes in the user interface (UI) through the Silverlight object model, you must delegate this work to the Dispatcher object of the thread that created the UI by calling BeginInvoke()". So, we should use the delegate to make changes on the UI.

Deploy and Test
We have two ways to deploy the XAP file in SharePoint environment.
One is, We can use SharePoint default location [\Templates\Layouts\ClientBin] and deploy the file there. Refer this location from the Silverlight web part.
Second is, We can use a SharePoint document library and deploy the file there. Refer this document library location file path while adding the silverlight web part. In this post, we will use the default location to deploy and test.

To deploy and test the code in SharePoint 2010 we need to use the SharePoint Silverlight web part [New web part added in this version].

The silverlight web part default location is "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\ClientBin". So, all the XAP files should be deployed to this location to use them in the silverlight web part.

29 comments:

Wow, This is the one of the great and excellent posts I like these days. The way you have written is very explanatory and easily understanding. I appreciate the work you are allocating for writing posts sharing knowledge. With this example, I am also one of the dev who created Silverlight app in SharePoint 2010. Awesome. Happy. :)

Silverlight Client Object Model is nothing but Silverlight programming and accessing objects. So, you can't do that with SharePoint designer. You should need Visual Studio 2010 to get this job done. Any way it's in BETA, I believe no problems to install it.

I cannot seem to get it deployed to the sharepoint site. When i enter the URL /_layouts/ClientBin/TEST.xapi get the error "Could not download the Silverlight application or the Silverlight Plugin did not load.

To re-configure the Web Part or to provide a different Silverlight application (.xap), open the tool pane and then click Configure."

Hi Art-Uhr,I have changed the code few days back and forgot to update the site url related information. Now the note section in the post is strike out.... So, no url and nothing to be changed. Just copy and paste directly... Thanks any way/

Great post indeed. I am at client side (meaning out of dev environment) and there is some discussion going at if SP2010 + Silverlight can be used as an application development tool which will be different from typical SharePoint look and feel. Can you please suggest any sites which were developed using above mentioned tools? I just want to give demo about the power of this combo.

i used your code to generate a silverlight application but i'm facing problem since last three to four days. the problem is that "OnSiteLoadSuccess" and "OnSiteLoadFailed" are never being called. i tried different ways but useless. kindly help..

i used your code to generate a silverlight application but i'm facing problem since last three to four days. the problem is that "OnSiteLoadSuccess" and "OnSiteLoadFailed" are never being called. i tried different ways but useless. kindly help..

yes i checked and event "btnLoadSite_Click()" gets called but "OnSiteLoadSuccess" and "OnSiteLoadFailed" are never being called. what could be the reason for this? any configuration am i missing or any other problem?

I beleive you are using the same code as is and you are deploying the code to a SharePoint server and using in a sharepoint site. Please find the full code here:http://praveenbattula.blogspot.com/2010/05/silverlight-client-object-model-samples.html

Venky,The error should not come. This is the first time I am seeing this kind of error. I believe as you are deployed with your credentials, I don't think this is not a permissions issue. And the document library you deployed is also in the same site site as you are using silverlight web part. And please get the complete code here and try:http://praveenbattula.blogspot.com/2010/05/silverlight-client-object-model-samples.html

Hi, I am new in silverlightI don't find the .dll files which should be located at Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\LAYOUTS\ClientBin.I don't even have this folders. It ends at \14 and then there is nothingCan I download those dlls somewhere?Thank you in advance