SharePoint Applied: SharePoint 2007 with WCF and Silverlight Silverlight 2 just went RTM. This product is unique because for the first time in the Microsoft world, you have .NET running cross platform, in a secure way, without all the deployment hassles. It has the ability to bring rich UI, right within the browser-much like Flash, but with more capabilities and a .NET heart. So, what does this mean to you-the SharePoint developer? Well, as I elucidated in my previous article, developing rich UIs in SharePoint 2007 isn’t exactly my idea of a good time! In fact, it is a bit like a 3-year old playing drums on your head all night long while his 7-year old sister is sticking chewing gum in your hair as you are trying to sleep because you have an early morning 7 AM meeting tomorrow in a recessionary economy. Take heart! The thin .NET 3.5 development model makes it all easier. In this article, I will illustrate an example that builds a Silverlight front-end to SharePoint functionality. The Silverlight control will work within the SharePoint context, and will thus allow you to access the SharePoint object model from the retarded (restricted) Silverlight CLR. Throughout this article, I will focus on illustrating the specific development steps I follow to make my development of Rich UIs within SharePoint a whole lot easier. Let’s begin with defining the problem first. Problem Definition I find the calendar list in SharePoint quite useful. In this article, I will build a Silverlight UI that shows me appointments for today while remaining under the current user’s context. These appointments will be stored in the calendar list and will be rendered in a rich Silverlight UI. At the very onset, this seems to tell me that the Silverlight UI running inside SharePoint would need to have full access to the SharePoint context. How in the world would I achieve that in the retarded Silverlight CLR? In order to achieve this, I will need to write: - A Silverlight .xap file. I will have to work with both Visual Studio 2008 SP1 with Silverlight tools, and Expression Blend 2 SP1 to craft this UI.
- A WCF service that works in-context, and retrieves the appointments under the security context of the currently logged in user. This WCF service will be consumed by the Silverlight UI using basicHttpBinding.
- A contract that specifies how the Silverlight application will talk to the WCF service. This is the same as the WCF contract.
As you can clearly tell, it all begins by defining the WCF contract. This snippet shows the contract: [ServiceContract] public interface IAppointments { [OperationContract] List<BO.Appointment> GetAppointments(); }
In the above code, the BO.Appointment is a custom business object I created as shown below: [DataContract] public class Appointment { [DataMember] public DateTime AppointmentTime { get; set; } [DataMember] public string Title { get; set; } }
With the contract defined, let me show you how to write up the pieces around the contract. Writing the Silverlight Application Now, the nice thing about the thin .NET 3.5 development model is that as long as I have a contract ironed out, I can take SharePoint completely out of the picture, and thus along with the virtual machine, the attaching to W3WP.exe, and the GAC DLL version whose breakpoint never seems to get hit. I’ll simply stub out the project on my Vista development machine as shown in Figure 1.  Figure 1: The Silverlight project structure.I created the project shown in Figure 1 using the Silverlight tools for Visual Studio 2008 SP1, and a WCF service library that I added to the project. As you can see, the MyServices class library is a WCF service library, which hosts a service in Appointments.cs that implements the contract I defined earlier. Since I am not running inside SharePoint so far, I will simply stub out fake appointments as shown in Listing 1. The code in Listing 1 is rather terse to write because I used a concept called Object and Collection Initializers introduced with C# 3.0. You can read more about them at http://blah.winsmarts.com/2006/05/21/demystifying-c-30--part-5-object-and-collection-initializers.aspx. Next I’ll start crafting up the UI of my Silverlight application. I need a DataBindable UI that I can bind the results of the WCF service call to. This snippet below shows how to implement it: <ListBox x:Name="UserAppointments" ItemTemplate= "{StaticResource ShowTime}"> </ListBox>
The “ShowTime” static resource is a DataTemplate that I defined as a resource, shown below: <DataTemplate x:Key="ShowTime"> <StackPanel Margin="10"> <ContentControl Template= "{StaticResource clockTemplate}" Width="120" Height="108" DataContext= "{Binding Path=AppointmentTime}"/> <TextBlock Text="{Binding Path=Title}" HorizontalAlignment="Center"/> </StackPanel> </DataTemplate>
For the eagle eyed amongst you, the ContentControl that points to a StaticResource is another application level resource I have created. This is nothing but just a ControlTemplate that takes a DateTime and renders a good looking Analog Clock out of it. You can find the source code for the clock ControlTemplate at http://blah.winsmarts.com/2008-10-Clock_Template_for_Silverlight.aspx. That’s it, my UI is done. It’s really that easy. Now I’ll give it some life with some data so you can see it running and in action. The Silverlight UI will get its data from the WCF service using basicHttpBinding. In order to do so, I first need to add a WCF service reference to my Silverlight application, as described at http://blah.winsmarts.com/2008-7-SilverLight_WCF_References_in_SharePoint_-_The_right_way.aspx. With the reference added, I can use the WCF service proxy to get Appointments data, and Databind the appointments to the UI that I created above. I can achieve this using the code shown in Listing 2. An important thing to note in Listing 2 is that you should always call WCF services from Silverlight UIs in an asynchronous manner. Failing to do so could lock up the browser, and the end user won’t be happy about that. When I compile and run the above application above, I see a UI as shown in Figure 2.  Figure 2: The Silverlight application running in Safari.Did you notice; I haven’t even touched SharePoint yet! So technically, the above could have been developed entirely by a non-SharePoint developer, good ones of which are in rather short supply. With my UI and WCF service skeleton done, let’s take the above to SharePoint. | & | | 
By: Sahil Malik
Sahil Malik is a Microsoft MVP, INETA speaker, a .NET author, consultant, and trainer, and a well-rounded overweight geek. He has a passion for SharePoint, data access, and application architecture.
Sahil loves interacting with fellow geeks in real time. His talks are full of humor and practical nuggets. His talks tend to get very highly charged, fast moving, and highly interactive.
You should check out his blog at http://blah.winsmarts.com
sahilmalik@gmail.com | Fast Facts | | Innovative solutions sometimes require multiple ingredients. Examine how to mix it up with SharePoint, WCF and Silverlight. | |
|