David Jacobus

SharePoint Consultant

  Home  |   Contact  |   Syndication    |   Login
  17 Posts | 0 Stories | 7 Comments | 0 Trackbacks

News

Hello,
My name is David Jacobus I am a former teacher of Computer Science and Mathematics, who is now working full time as a SharePoint Consultant!

Article Categories

Archives

Post Categories

Sunday, August 01, 2010 #

   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


 

I started training on SharePoint 2010 about 3 months ago and I decided that I needed to blog about the ease of developing Silverlight applications with Visual Studio 2010 for SharePoint 2010. In Visual Studio 2008 the methodology would be to:

1. Create a Silverlight application

2. Create the XAP file

3. Move the XAP file into a SharePoint document library or Layouts folder

4. Put a Silverlight Web part on a SharePoint page (not OOTB)

a. Link the Silverlight web part to the XAP file

5. Debug the web part by “Attaching to Process”

6. When the XAP File changed:

a. Copy it back to the document library or Layout folder

Using Visual Studio 2010:

1. Create a SharePoint Blank application

2. Add a Silverlight Web Application

3. Add a module to the SharePoint

a. Add the output of the Silverlight Project to the module

4. Put a Silverlight Web part on a SharePoint page (on my dev. box I set the home page, OOTB)

a. Link the Silverlight web part to the XAP file

5. Hit the F5 key and you are up and debugging

The important distinction here is that you are up and debugging with one keystroke.

I will walk you through the process of setting up a SharePoint 2010/Silverlight project

1. Create a blank SharePoint project in 2010:

1

2. Set it as a SandBoxed solution if applicable

2

3. Add a Silverlight application

3

4. Add a module to the SharePoint application

4

4a

5. Link the output of the Silverlight Project to the SharePoint Module By clicking the module:

5

6. In the properties window click the ellipse’s next to Project Output references

6

7. In the window that appears click add

7

8. In the Properties pane Click Deployment Type and choose Element File

8

9. In the Properties pane Click Project Name and choose the output project to be the Silverlight Web Project (not the application test web site)

9

10. Immediately the module will get the Silverlight XAP file added to it!

10

11. Delete the file reference to the sample.txt file from the module

11

12. Add a URL to the module in this case documents

12

13. Add some content to the Silverlight Project ( I will Add Hello World)

13

13a

14. Configure the web part on the home page/Development site

14

15. Choose the SharePoint Project as the startup project and hit F5. If the web part is on the home page it will come up debugging else browse to the page and “click deploy” on the Build Menu and then refresh the page. Here is the page with Hello World:

15

16. Adding some new text to debug:

16

17. Here is the page being debugged (not using F5) as I don’t have the test web part on the home page. I just attach to process: Using Silverlight as the key to which process to debug:

17

18. Hitting the Breakpoint:

18

19. The new text:

19