Content by Category
.NET 1.x
.NET 2.0
.NET 3.0
.NET 3.5
.NET 4.0
.NET 4.5
.NET Assemblies
.NET Framework
.NET Getting Started
Accessibility
ADO.NET
Advertorials
Agile Development
AJAX
Amazon Web Services
Analysis Services
Android
Architecture
Arduino
ASP .NET Web API
ASP.NET
ASP.NET MVC
ASP.NET WebForms
Azure
B2B (Business Integration)
BDD
Big Data
Bing
BizTalk
Book Excerpts
Build and Deploy
Business Intelligence
C#
C++
ClickOnce
Cloud Computing
Code Contracts
CODE Framework Info - non Technical
CODE on the Road!
COM+
Community
Conferences
Continuous Integration
Crystal Reports
CSLA.NET
CSS
Data
Debugger
Design Patterns
Development Process
Display Technologies
Distributed Computing
Document Database
DotNetNuke
DSL
Dynamic Languages
Dynamic Programming
Editorials
Enterprise Services ("COM+")
Entity Framework
Events
Expression Blend
F#
Fox to Fox
Frameworks
Functional Programming
Git
Graphics
HTML 5
Internet Explorer 8.0
Interviews
IOS
iPhone
Iron Ruby
Java
Java Script
JavaScript
jQuery
JSON
Lightswitch
LINQ
Linux
LUA
Mac OS X
MDX
Messaging
Metro
Microsoft Application Blocks
Microsoft Business Rules Framework
Microsoft Dynamics
Microsoft Expression
Microsoft Office
Mobile Development
Mobile PC
Mono
MsBuild
MVVM
MySQL
Network
NHibernate
node.js
NOSQL
Nuget
Object Oriented Development
Objective C
Odata
OLAP
Open Source
Opinion
Opinions
Oracle
ORM
Other Languages
Parallel Programming
Patterns
PHP
Podcasts
Post Mortem
PowerPoint
Print/Output
Prism
Product News
Product Reviews
Project Management
Prolog
Python
Q&A
Rails
Rake
Razor
Reporting Services
REST
RIA Services
Ruby
Ruby on Rails
Scheme
Search
Security
Services
SharePoint
SignalR
Silverlight
SOA
Social Networks
Software & Law
Software Business
Source Control
Speech-Enabled Applications
SQL Server
SQL Server 2000
SQL Server 2005
SQL Server 2008
SQL Server 2012
SQL Server CE/AnyWhere/Mobile/Compact
SSIS
Subversion
Sync Framework
Tablet PC
TDD
Team System
Techniques
Testing and Quality Control
TFS
Tips
TypeScript
UI Design
UML
User Groups
VB Script
VB.NET
Version Control
VFP and .NET
VFP and SQL Server
Virtual Earth
Vista
Visual Basic
Visual Basic 6 (and older)
Visual FoxPro
Visual Studio .NET
Visual Studio 11
Visual Studio 2005
Visual Studio 2008
Visual Studio 2010
Visual Studio 2011
Visual Studio 2012
Visual Studio Tools for Office
VSX
WCF
Web Development (general)
Web Services
WebMatrix
WF
Whitepapers
Windows 7
Windows 8
Windows Azure
Windows Live
Windows Phone 7
Windows Phone SDK
Windows Server
Windows Vista
WinForms
WinRT
Workflow
WPF
XAML
Xiine Documentation
XML
XNA
XSLT



Component One


LearnNow
 


Learn Now

Reader rating:
Click here to read 4 comments about this article.
Article source: CoDe (2009 Mar/Apr)


Article Pages:  1  2 - Next >


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:

  1. 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.
  2. 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.
  3. 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 { 
     getset; }
    [DataMember]
    public string Title { getset; }
}

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.

Click for a larger version of this image.

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.

Click for a larger version of this image.

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.



Listing 1: The stubbed out WCF method implementation
public List<BO.Appointment> GetAppointments()
{
    List<BO.Appointment> toReturnAppts = 
        new List<BO.Appointment>();
    toReturnAppts.Add(
      new BO.Appointment { 
          Title = "First Appointment", 
          AppointmentTime = DateTime.Now });
    toReturnAppts.Add(
      new BO.Appointment { 
          Title = "Second Appointment", 
          AppointmentTime = DateTime.Now });
    toReturnAppts.Add(
      new BO.Appointment { 
          Title = "Third Appointment", 
          AppointmentTime = DateTime.Now });

    return toReturnAppts;
}


Listing 2: Code behind for the Silverlight application
public partial class Page : UserControl
{
    public Page()
    {
        InitializeComponent();
        EndpointAddress endPointAddress = 
           new EndpointAddress(
           "http://localhost:52178/WCF/Service1.svc");
        BasicHttpBinding basicHttpBinding = 
           new BasicHttpBinding();
        AppointmentsClient client = 
           new AppointmentsClient(
               basicHttpBinding, endPointAddress);
        client.GetAppointmentsAsync();
        client.GetAppointmentsCompleted += 
           new EventHandler<GetAppointmentsCompletedEventArgs>(
           client_GetAppointmentsCompleted); 
    }

    void client_GetAppointmentsCompleted(
          object sender, 
          GetAppointmentsCompletedEventArgs e)
    {
        UserAppointments.ItemsSource = e.Result;
    }
}


Article Pages:  1  2 - Next Page: 'Enter SharePoint: Authoring the WCF Service' >>

Page 1: SharePoint Applied: SharePoint 2007 with WCF and Silverlight
Page 2: Enter SharePoint: Authoring the WCF Service

How would you rate the quality of this article?
1 2 3 4 5
Poor      Outstanding

Tell us why you rated the content this way. (optional)

Average rating:
4.9 out of 5

14 people have rated this article.

Hacker Halted

      Sharepoint TechCon

 

Hacker Halted