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 – http://msdn.microsoft.com/en-us/library/bb862916(office.12).aspx

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.

Code-Snippet-01

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.

Code-Snippet-02

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.

Code-Snippet-03

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

Code-Snippet-04

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

Code-Snippet-05

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;
   8:  
   9:                 _listProxy.GetListCollectionCompleted += new SharePoint3ListsService.GetListCollectionCompletedEventHandler(_listProxy_GetListCollectionCompleted);
  10:                 _listProxy.GetListCollectionAsync();
  11:             }
  12:             catch (Exception ex)
  13:             {
  14:                 //
  15:             }
  16:         }
  17:  
  18:         void _listProxy_GetListCollectionCompleted(object sender, SharePoint3ListsService.GetListCollectionCompletedEventArgs e)
  19:         {
  20:             try
  21:             {
  22:                 XElement results = XElement.Parse(e.Result.OuterXml, LoadOptions.None);
  23:  
  24:                 var linqQuery = from x in results.Descendants() select x;
  25:  
  26:                 List<SPList> items = new List<SPList>();
  27:  
  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:                 }
  39:  
  40:                 var action = new Action<List<SPList>>(UpdateListsLibrariesBox);
  41:  
  42:                 Dispatcher.BeginInvoke(DispatcherPriority.Input, action, items);
  43:             }
  44:             catch (Exception ex)
  45:             {
  46:                 //
  47:             }
  48:         }
  49:  
  50:         void UpdateListsLibrariesBox(List<SPList> lists)
  51:         {
  52:             listBoxLibraries.ItemsSource = lists;
  53:         }
  54:  
  55:         void worker_ListsLibrariesProgressChanged(object sender, ProgressChangedEventArgs e)
  56:         {
  57:  
  58:         }
  59:  
  60:         void worker_RunListsLibrariesWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  61:         {
  62:  
  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.

Advertisements

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:

WordPress.com Logo

You are commenting using your WordPress.com 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