Before SharePoint Client Object Model – Using Web Services to Consume SharePoint 3.0 Data in WPF

Technorati Tags: ,

I am thinking about posting a few entries on how we would go about consuming SharePoint data. There are many options out there. I’ll start with the good old web services way. Using SharePoint ASMX web services will allow you to consume List and Site Data as well as do many administrative tasks, such as creating sites and pages to web part and list management.

Reference to v3.0 and MOSS 2007 web services –

Scenario – View all lists and libraries on a SharePoint site using the Lists web service [http://wsssite/_vti_bin/Lists.asmx]

I’ve got a WPF desktop application with a single Page. This page will need to have a TAB control. One of its Tabs will need to list all available SharePoint Lists and Libraries on a given site.

Solution – Overall approach and steps

Step One – Firstly, assuming you’ve got all that you need. A WSS 3.0 or a MOSS 2007 site with some lists and libraries. By default you should have some. You also got Visual Studio and a WPF Desktop application built. You go ahead and add a web service reference to the Lists ASMX service mentioned above. Go a step further and define a class that will eventually hold your List data.

Here’s a code snippet to a WPF Tab Control with one Tab in it.


Step Two – Secondly, we start in the code behind. We’ll need few things. I went ahead and defined a class to hold my List Data. Here it is.


Step Three – Thirdly make sure you’ve got your web service referenced and such. Let’s add some code to call on it during the WPF page initialization and loading.


In addition to service reference, you will need references to LINQ and Threading.


Step Four – Wire it up and load the list of all libraries available on the site


Notice the use of Threading and Asynchronous calls into the web service. Why, well, simply put so the WPF UI doesn’t get stuck on the call to the web service and you could load the Tab control without affecting the user experience.

Step Five – Code the actual work to be done

   1: void worker_DoListsLibrariesWork(object sender, DoWorkEventArgs e)
   2:         {
   3:             try
   4:             {
   5:                 _listProxy = new SharePoint3ListsService.Lists();
   6:                 _listProxy.Url = listsWebServiceRelativeUrl;
   7:                 _listProxy.Credentials = credentials;
   9:                 _listProxy.GetListCollectionCompleted += new SharePoint3ListsService.GetListCollectionCompletedEventHandler(_listProxy_GetListCollectionCompleted);
  10:                 _listProxy.GetListCollectionAsync();
  11:             }
  12:             catch (Exception ex)
  13:             {
  14:                 //
  15:             }
  16:         }
  18:         void _listProxy_GetListCollectionCompleted(object sender, SharePoint3ListsService.GetListCollectionCompletedEventArgs e)
  19:         {
  20:             try
  21:             {
  22:                 XElement results = XElement.Parse(e.Result.OuterXml, LoadOptions.None);
  24:                 var linqQuery = from x in results.Descendants() select x;
  26:                 List<SPList> items = new List<SPList>();
  28:                 foreach (XElement x in linqQuery)
  29:                 {
  30:                     if (x.Attribute("Title").Value != null)
  31:                     {
  32:                         items.Add(new SPList
  33:                             {
  34:                                 Title = (x.Attribute("Title").Value != null) ? x.Attribute("Title").Value : null,
  35:                                 DefaultViewUrl = (x.Attribute("DefaultViewUrl").Value != null) ? x.Attribute("DefaultViewUrl").Value : null,                                
  36:                             });
  37:                     }                   
  38:                 }
  40:                 var action = new Action<List<SPList>>(UpdateListsLibrariesBox);
  42:                 Dispatcher.BeginInvoke(DispatcherPriority.Input, action, items);
  43:             }
  44:             catch (Exception ex)
  45:             {
  46:                 //
  47:             }
  48:         }
  50:         void UpdateListsLibrariesBox(List<SPList> lists)
  51:         {
  52:             listBoxLibraries.ItemsSource = lists;
  53:         }
  55:         void worker_ListsLibrariesProgressChanged(object sender, ProgressChangedEventArgs e)
  56:         {
  58:         }
  60:         void worker_RunListsLibrariesWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  61:         {
  63:         }

Summary – This is just one option of how to consume SharePoint data in your client application. Next time I’ll try to demonstrate how to do that using SharePoint 2010 Client Object Model.

Take it easy.


About jharbieh

I'm an IW Solutions Architect with background in requirements gathering, planning, design, architecture, and development (not necessarily in the right order). Currently, my focus is on the Microsoft Cloud, Productivity and Collaboration space. Hope you enjoy what I write about here. Thanks for visiting. Johnny Harbieh
This entry was posted in SharePoint Development. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s