Silverlight Client Object Model in SharePoint 2010 – Part III

Posted by: Pravinkumar Dabade , on 5/8/2011, in Category SharePoint
Views: 58976
Abstract: In this article, we will see how to use the Silverlight Client Object Model in SharePoint 2010. We will create a Silverlight Application, access the SharePoint data and then deploy it to a SharePoint site. We will also see how to write CAML queries to filter the SharePoint List data.

In this article, we will see how to use the Silverlight Client Object Model in SharePoint 2010. We will create a Silverlight Application, access the SharePoint data and then deploy it to a SharePoint site. We will also see how to write CAML queries to filter the SharePoint List data.

If you haven’t already read, Part I and II of this series can be found over here Silverlight Integration with SharePoint 2010 – Part I and Read SharePoint 2010 List in Silverlight using REST – Silverlight with SharePoint 2010 - Part I

To start with, first let’s create a Silverlight application and give a name of ‘SLClientOMExample’ as shown below –

clip_image001

Now let’s add a few references in the Silverlight project to the following libraries –

 

  1. Microsoft.SharePoint.Client.Silverlight.dll.
  2. Microsoft.SharePoint.Client.Silverlight.Runtime.dll.

You can find these APIs under ‘C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\ClientBin’.

Now let’s design our Silverlight application. Replace the <Grid></Grid> with the following code –

<Grid x:Name="LayoutRoot" Background="Black">

<ListBox x:Name="spLists" Height="300" Width="400"/>

</Grid>

First write the code to fetch all the SharePoint lists and display in the Listbox. To do so, we will define the two variables at class level as shown below –

ClientContext context;

Web spWeb;

Now write the following code in the constructor of our class (code behind) –

this.Loaded += new RoutedEventHandler(MainPage_Loaded);

Now add a method MainPage_Loaded as shown below –

clip_image002

In the method shown above, we are declaring an instance of ClientContext class which takes an URL of SharePoint site in which we will be using this Silverlight application. From the ‘ClientContext’ class we are loading a SharePoint site and the all the SharePoint lists which are available under it.

The ‘ExecuteQueryAsync method takes the addresses of two methods using delegates, ‘ClientRequestSucceededEventHandler’ and ‘ClientRequestFailedEventHandler’ respectively. When the call to server is successful, it calls a method ‘OnRequestSucceeded’. The code of this method is as shown below –

clip_image003

The method of BeginInvoke takes the address of a method ‘DisplayLists’. The method is shown below –

clip_image004

Now let’s write some code for the method ‘OnRequestFailed’ as shown below –

clip_image005

Deploy this Silverlight application to our SharePoint site as a ‘Sandbox Solution’. To deploy the Silverlight application, I have step-by-step article written on www.devcurry.com. Please follow the instructions written in an article Deploy Silverlight Application using SharePoint 2010 Project.

Once you deploy the Silverlight application and configure the ‘.XAP’ file with the Silverlight web part, the final output should look like the following –

clip_image006

 

Access SharePoint List data and display using Silverlight

Now let’s access the data of a SharePoint list ‘Customers’ and display it in a ‘Datagrid’ control of the Silverlight application. For this demonstration you will have to create a Customers list as mentioned below –

clip_image007

Let’s create a Silverlight Application and give it any name. Once your project is created, add the two DLL references as shown in the above steps –

  1. Microsoft.SharePoint.Client.Silverlight.dll.
  2. Microsoft.SharePoint.Client.Silverlight.Runtime.dll.

Add a ‘Datagrid’ control in our MainPage.xaml. Replace the <Grid></Grid> with below code –

clip_image008

Now define four variables at class level in our code behind as shown below –

clip_image009

In the constructor of our class, let’s write the following code –

this.Loaded += new RoutedEventHandler(MainPage_Loaded);

Now let’s write code for our MainPage_Loaded method as shown below –

clip_image010

The above code is somewhat similar to our last demonstration. But the difference is in the above code is that we are loading a specific list called ‘Customers’ and not all the lists from SharePoint site. Also we are writing a CAML (Collaborative Application Markup Language) query to fetch the data from a specific City. In our demo I am using ‘Pune’.

Now let’s write two methods ‘OnRequestSucceeded’ and ‘OnRequestFailed’ as shown below –

clip_image011

The ‘BeginInvoke’ method in our ‘OnRequestSucceeded’ method takes an address of DisplayListData’ method. This method is used to fetch the data from specific columns of the Customers list and display in a Silverlight ‘Datagrid’ control.

Add a class with the name ‘SPCustomers’ which will match the schema of our SharePoint list as shown below –

clip_image012

Now write the following code in our ‘DisplayListData’ method, as shown below –

clip_image013

The above method creates a collection of ‘SPCustomers’ class. Then by using a For-Each loop, we are accessing the SharePoint list data (Customers List) which we queried above ,and then adding it to our collection by assigning the values of the ‘ListItem’, to the properties of our ‘SPCustomer’ class.

Finally, we are assigning the collection to our DataGrid as a data source.

Now deploy and test your application as mentioned in the article Deploy Silverlight Application using SharePoint 2010 Project

clip_image014

Conclusion – In this article, we have seen how to use ‘Silverlight Client Object Model’ to fetch the SharePoint Lists and List data. Then we have seen how to deploy the Silverlight applications as a Sandbox solution, to our SharePoint site.

The entire source code of this article can be downloaded over here

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
Pravinkumar, works as a freelance trainer and consultant on Microsoft Technologies. He is having over 10 years of experience in IT and is also a Microsoft Certified Trainer(MCT). He has conducted various corporate trainings on all versions of .NET Technologies including .NET, SharePoint Server, Microsoft SQL Server, Silverlight, ASP.NET, Microsoft PerformancePoint Server 2007 (Monitoring). He is passionate about learning new technologies from Microsoft. You can contact Pravinkumar at dabade[dot]pravinkumar [attherate] gmail[dot]com


Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by Markus on Friday, June 1, 2012 2:44 PM
What sort of security needs to be setup to access the database with the caml query? in iis and in sql, in the application, wherever. I get a 'security error' from system.security.securityexception when the code gets to ExecuteQueryAsync. Other than that, which may be a missing piece in your article, i found it to be well presented, some good learning, and well detailed.
Comment posted by Priyanka on Saturday, August 11, 2012 6:40 AM
Hello,

When I deploy the result ,I get a blank screen in my sharepoint site.
My version details are

VS studio 2010 ->version added silverlight 4
running in sharepoint foundation site
silverlight 5 is installed in my system

Can you please help me out why I m not able to load the silverlight app?

Thanks
Comment posted by harsha on Wednesday, September 17, 2014 6:23 PM
very useful..thanks for sharing this silverlight example