Geeks With Blogs
David Jacobus SharePoint Consultant
   I have spent some time, around all 6 of the SharePoint 2010 data access technologies

 

1.  Using SharePoint standard web services

2.  SharePoint Client Object Model

3.  Silverlight SharePoint Client Object Model

4.  Data Services Client Model

5.  OWSRV.DLL

6.  SharePoint Object Model

 

Silverlight is a client application so the data access technologies applicable are:

1. SharePoint web Services

2. Silverlight Client Object Model

3. Data Services Client Model

4. OWSSRV.DLL

These 4 have many features in common most of which centers around all queries need to be  asynchronous  so the queries must have delegates for begin and end. 

In Comparing and Contrasting them against each other.  Much of the technology for Data Services, Silverlight Client Object Model, and SharePoint Client Object Model is new and the documentation is incomplete.   I like the Data Services technology as this will be a common pattern across all of the Microsoft stack.  In addition it seems easier to put together an application with it.

 

The common LINQ to SharePoint scenario is to use SPMetal on the current site and use the strong typing of SharePoint Lists which requires some work from the developer to put together.  In contrast, adding a service reference to http://<<your site>>/{vti_bin/listsdata.svc will automatically add a strongly typed class for all lists in the development site.  Much of the hype about data services centers around the URL syntax of the queries available and the output xml which is much like the way SharePoint data was consumed via web services or OWSRV.DLL: read the xml into an xml document and then parse the result.  In reality, for a real application, that happens behind the scenes. The developer just needs to access data using LINQ to SharePoint as with using the SharePoint Client Object Model or Silverlight Client Object Model.

 

I will use this technology with Silverlight and SharePoint 2010,  In a previous blog I showed you how easy to integrate Silverlight and a SharePoint project together  using Visual Studio 2010.  SharePoint 2010, Silverlight and Visual Studio

 

I will use the same demo project I used with the previous blog and add some code accessing list data using data services.

 

1.  Add a Service Reference to the project

2

 

1

 

 

 

2,  Choose Show Data Sources from the toolbar. See all the lists in the site have a class like spmetal which  have  strongly typed classes.

3

3.  I am going to choose a list with a lot of data to add to Silverlight project, I’ll just drag it onto the Silverlight screen and get a Silverlight Data Grid and edit the columns for viewing/demonstration purposes.: MasterPageGallery

editcolumns

datagrid

 

4.  Basically we have the shell to display the data; However now we need to get the list data and then display it.  When, the datagrid is added to the Silverlight User Control it adds some demo code to the silverlight user control loaded event which guides us in how and where to load the data:

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {

            // Do not load your data at design time.
            // if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            // {
            //  //Load your data here and assign the result to the CollectionViewSource.
            //  System.Windows.Data.CollectionViewSource myCollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["Resource Key for CollectionViewSource"];
            //  myCollectionViewSource.Source = your data
            // }
        }
5.Add a class to make handling the data easier. This class is just a couple of properties which make data access easier (Thanks MSDN):
datacontextclass 
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DemoSilverlightApplication.ListDataService;
using System.Data.Services.Client;
namespace DemoSilverlightApplication
{
    public class DataServicesContext
    {
        HomeDataContext _ctx;
        //used by Data Services got a strongly typed Observeable Collection
        DataServiceCollection<MasterPageGalleryItem> _masterPageGallery;
        public HomeDataContext DataContext
        {

            get
            {
                if (_ctx == null)
                {
                    _ctx = new HomeDataContext(
                        new Uri("http://djacobus01/_vti_bin/listdata.svc", UriKind.Absolute
                        )
                     );
                }
        
                return _ctx;
            }
        }

        public DataServiceCollection<MasterPageGalleryItem> MasterPageGalleryDataServiceColl
        {
            get
            {
                if (_masterPageGallery == null)
                    _masterPageGallery = new DataServiceCollection<MasterPageGalleryItem>(DataContext);
                return _masterPageGallery;


            }
        }
    }
}

Where most of the class can be gleaned from intellisense.

 

6.  Add a data context to the MainPage.xaml.cs:

 

        private DemoSilverlightApplication.DataServicesContext _currentContext;
        public DemoSilverlightApplication.DataServicesContext CurrentContext
        {
            get
            {
                if (_currentContext == null)
                    _currentContext = new DemoSilverlightApplication.DataServicesContext();
                return _currentContext;
            }
        }

 

contextcode

 

6.  We need to add a method to get all the master pages:

 

 public void GetAllMasterPageItems()
        {

            var query = (
                   from Item in CurrentContext.DataContext.MasterPageGallery
                   where Item.Id > 0
                   select Item
            ) as DataServiceQuery<MasterPageGalleryItem>;
            query.BeginExecute(
                (IAsyncResult asyncResult) => Dispatcher.BeginInvoke(() =>
                {
                    DataServiceQuery<MasterPageGalleryItem> queryResults = asyncResult.AsyncState as DataServiceQuery<MasterPageGalleryItem>;
                    if (queryResults != null)
                    {
                        CurrentContext.MasterPageGalleryDataServiceColl.Clear(true);

                        CurrentContext.MasterPageGalleryDataServiceColl.Load(
                             queryResults.EndExecute(asyncResult)
                        );
                    }
                })
                , query
             );

            myData = CurrentContext.MasterPageGalleryDataServiceColl;
        }

7.  The above method is the correct way to retrieve list data using Data Services.  Therefore using this project as a template you can start using this Data Access Technology.

8.  Here is the page with the Silverlight web part

 

page

 

9.  Here is a link to the project: Demo project   Since all SharePoint 2010 site collections have a master page gallery the source code should work work with an update to the service reference, Update the data context class with the url to your server and update the SharePoint Project with the URL of your server:

 

 

updatecontext

spproject

Posted on Sunday, August 1, 2010 11:35 PM | Back to top


Comments on this post: Silverlight and SharePoint Data Access

# re: Silverlight and SharePoint Data Access
Requesting Gravatar...
What is myData at the last - I'm getting compilation error in that.
Left by Srikanth on Jan 04, 2011 7:45 AM

# re: Silverlight and SharePoint Data Access
Requesting Gravatar...
Very informative article!! Its really help for beginner as well as developer. Check out this link too,
http://mindstick.com/Articles/9ac47341-30ea-409c-8c39-4d2740929720/?Using%20LINQ%20to%20access%20SharePoint%20list%20Data
it is also helped me lot.

Thanks Everyone!!
Left by Ajay Singh on Dec 19, 2011 11:45 PM

Your comment:
 (will show your gravatar)


Copyright © David Jacobus | Powered by: GeeksWithBlogs.net