Monday March 9, 2009

In Part Two of this series we expanded on the out-of-the-box connectivity to the SharePoint Lists web service and added the ability to dynamically connect to any SharePoint server URL. In this article we will make a more complex call to retrieve list detail and show how data retrieved from the SharePoint lists web service can be bound to Silverlight controls.

The first step we are going to need to do is enhance our existing sample code base and update the user interface to give us a little more flexability to select a list and get some details. So lets start with our previous code base which can be found in Part Two or you can get it directly here: PraticalSiverlightAndSharePointIntegration.Part2.zip.

First lets split our single output text box into two sections, on the left a list of list, and on the right a original output text box for additional detail about the list.

Since we want to let Silverlight to most of the work in displaying our “list of lists” all we need to do here is to bind the ItemsSource property of the Silverlight Listbox to the collection of list elements returned by SharePoint. In this case we are only interested in the individual List elements so we used the Linq Descendants function to get the collection of List elements as follows:

Now compiling and running the application we should see something like the following when “Go” is pressed:

Ok so the output still isn’t very appealling… We will need to add a little more xaml code to our page to take advantage of Silverlight’s data templating. In addition to xaml code, the out-of-the-box data binding doesn’t understand how to read directly from XML nodes, so to start with we will use an IValueConverter class that will assist the Silverlight binding in accessing data directly from the XElement classes that are returned by the call to Descendants. Below is the code for this new helper class:

Now compiling and running the application, should produce results simliar to this:

So far so good. Now lets get into make our “list of lists” clickable so that we can see more detail in our output text box when a list item is selected. First we modify the xaml code to include a SelectionChanged event handler then we add the following code to handle the event in the code behind:

Note in the code above: Now that we are requiring a bound ListsSoapClient in more than one place in our application, it makes sense to place the code for doing this in it’s own method called CreateListsSoapClient.

Running our application now will give us the same results when we press “Go”, but when we select a list item, the right handle textbox panel should now show the results of a call to GetListAsync. GetListAsync returns details about each list selected.

Selecting a list item should result in something like the following image: