ARTICLE

How to display the SharePoint 2010 list items in the Silverlight Datagrid using Client Object Model

Posted by Vijai Anand Articles | SharePoint 2010 December 24, 2010
In this article we will be seeing how to display the SharePoint 2010 list items in the Silverlight Datagrid using Client Object Model.
Reader Level:

In this article we will be seeing how to display the SharePoint 2010 list items in the Silverlight Datagrid using Client Object Model.

Steps Involved:

  • Open Visual Studio 2010.
  • Go to File => New => Project.
  • Select Silverlight Application template and enter the Name for the project.
  • Click Ok.
  • Open MainPage.xaml and replace the code with the following one.
<UserControl xmlns:my="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"  x:Class="SLGetFilesFromDL.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"      
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400"> 
    <Grid x:Name="LayoutRoot" Background="White">
        <my:DataGrid x:Name="dgFilesFromDL" Height="200" Width="300" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" AutoGenerateColumns="True"
                           Background="#FFEBD0D0" AlternatingRowBackground="#FFF8ECEC">
            <my:DataGrid.RowGroupHeaderStyles>
                <Style TargetType="my:DataGridRowGroupHeader">
                    <Setter Property="PropertyNameVisibility" Value="Collapsed" />
                    <Setter Property="Background" Value="#FF112255" />
                    <Setter Property="Foreground" Value="#FFEEEEEE" />
                    <Setter Property="SublevelIndent" Value="15" />
               
</Style>
                <!-- Style for groups under the top level -->
                <Style TargetType="my:DataGridRowGroupHeader">
                    <Setter Property="Background" Value="#44225566" />
                </Style>
            </my:DataGrid.RowGroupHeaderStyles>
        </my:DataGrid>     
       
<data:DataPager Source="{Binding  Path=ItemsSource, ElementName=dgFilesFromDL}" PageSize="7" Margin="166,0,50,0"></data:DataPager>
    </Grid>
</
UserControl>

Open MainPage.xaml.cs and replace the code with the following.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.SharePoint.Client;
using System.Threading;
using
System.Windows.Data;
 
namespace SLGetFilesFromDL
{
    public partial class MainPage : UserControl
    {     
        private ListItemCollection _empDetails;
 
        public class Project
        {
            public String empName { get; set; }          
        }
        public MainPage()
        {
            InitializeComponent();
            ClientContext context = new ClientContext(ApplicationContext.Current.Url);
            context.Load(context.Web);
            List empDetails = context.Web.Lists.GetByTitle("Employee Details");
            context.Load(empDetails);
 
            CamlQuery query = new Microsoft.SharePoint.Client.CamlQuery();
            string camlQueryXml = "<View><ViewFields>" +
                "<FieldRef Name=\"Title\" />" +
                "</ViewFields></View>";
 
            query.ViewXml = camlQueryXml;
            _empDetails = empDetails.GetItems(query);
            context.Load(_empDetails);
            context.ExecuteQueryAsync(new ClientRequestSucceededEventHandler(OnRequestSucceeded), null);
        }
 
        private void OnRequestSucceeded(Object sender, ClientRequestSucceededEventArgs args)
        {
            // This is not called on the UI thread.
            Dispatcher.BeginInvoke(BindData);
        }
 
        private void BindData()
        {
          
            List<Project> projects = new List<Project>();
            foreach (ListItem li in _empDetails)
            {
                projects.Add(new Project()
                {
                    empName = li["Title"].ToString(),                 
                });
            }
            PagedCollectionView pcv = new PagedCollectionView(projects);
            this.dgFilesFromDL.ItemsSource = pcv;         
            pcv.SortDescriptions.Add(new System.ComponentModel.SortDescription("empName", System.ComponentModel.ListSortDirection.Ascending));
        }
    }
}
 
Refer to the following link Integrating Silverlight in SharePoint 2010 to add Silverlight webpart in the SharePoint 2010.

Output:

Names are sorted in Ascending Order:

1.gif

Names are sorted in Descending Order:

2.gif
 

Login to add your contents and source code to this article
post comment
     

after i pasted the above code, using Microsoft.SharePoint.Client; PagedCollectionView,ListItemCollection etc. are not being recognized even after adding respective dlls

Posted by rohith bs Nov 16, 2012

Hey I followed same steps as you mentioned but when it executes below statement context.ExecuteQueryAsync(new ClientRequestSucceededEventHandler(OnRequestSucceeded), new ClientRequestFailedEventHandler(failed)); it throws this exception."ServerVersion = 'context.ServerVersion' threw an exception of type 'Microsoft.SharePoint.Client.PropertyOrFieldNotInitializedException'" And it doesn't go in any success and failed event handlers. What could be the issue? My code exactly look like:http://msdn.microsoft.com/en-us/library/ff728647.aspx Thanks in advance. Waiting for your response.

Posted by Kunajn Sanghavi Jul 29, 2011

ApplicationContext.Current.Url it is giving null in the runtime so i hardcoded that url and gave my sharepoint site... Thanks Reena

Posted by ravi Jan 08, 2011

using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using Microsoft.SharePoint.Client; using System.Threading; using System.Windows.Data; namespace SLGetFilesFromDL { public partial class MainPage : UserControl { private ListItemCollection _empDetails; public class Project { public String empName { get; set; } } public MainPage() { InitializeComponent(); ClientContext context = new ClientContext("http://intranet.wingtip.com"); context.Load(context.Web); List empDetails = context.Web.Lists.GetByTitle("Employee List"); context.Load(empDetails); CamlQuery query = new CamlQuery(); string camlQueryXml = "<View><ViewFields>" + "<FieldRef Name=\"Title\" />" + "</ViewFields></View>"; query.ViewXml = camlQueryXml; _empDetails = empDetails.GetItems(query); context.Load(_empDetails); context.ExecuteQueryAsync(new ClientRequestSucceededEventHandler(OnRequestSucceeded), null); } private void OnRequestSucceeded(Object sender, ClientRequestSucceededEventArgs args) { // This is not called on the UI thread. Dispatcher.BeginInvoke(BindData); } private void BindData() { List<Project> projects = new List<Project>(); foreach (ListItem li in _empDetails) { projects.Add(new Project() { empName = li["Title"].ToString(), }); } PagedCollectionView pcv = new PagedCollectionView(projects); this.dgFilesFromDL.ItemsSource = pcv; pcv.SortDescriptions.Add(new System.ComponentModel.SortDescription("empName", System.ComponentModel.ListSortDirection.Ascending)); } } } I used the above code and i am getting that error... I have title field in the sharepoint List.. Thanks Reena

Posted by ravi Jan 08, 2011

Hi ravi...have u given the caml query (string camlQueryXml = "<View><ViewFields>" +"<FieldRef Name=\"Title\" />" + "</ViewFields></View>";) and list name properly...i think u didnt use the proper column name in the code..thanks...vijai

Posted by Vijai Anand Jan 06, 2011
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter