Print This Post

SharePoint and Silverlight- a perfect match

In a previous article I wrote about one way to liven up SharePoint (or Office Live in that case) sites by creating Flash charts. In that instance javascript was used to query the SharePoint web services to collect list data and populate a chart. The data was provided to the chart component by means of an XML document. Now that Microsoft Silverlight is available developing this type of component should become a great deal easier.

The Silverlight client runtime renders XAML content which it retrieves from the server. This XAML could easily come from a query on a SharePoint list and provided to the Silverlight client via a simple ASP.Net handler. This article will describe the technique for creating just such a handler.

First of all we need to create an ASP.Net handler (an .ashx file). This handler will perform a query on our SharePoint site using the SPQuery class and will then return XAML in the response stream. The handler simply gets the required SharePoint list data and transforms it into XAML using an XSLT file or other method. So long as the handler sets the response type to text/xml and returns a stream of valid XAML then the Silverlight control will be able to render it. The handler itself will need to be placed inside the layouts folder of your SharePoint installation- the recommended way to make this happen would be via a feature. The bare bones of a handler to query a SharePoint list and return XAML is shown below:

<%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ WebHandler Language="C#" Class="GetOrgChart" %>

using System;
using System.IO;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.Net;
using Microsoft.SharePoint;

public class GetOrgChart : IHttpHandler
{
    string TransformToXaml(SPListItemCollection items)
    {
        // Your list items get converted to XAML here....
        return "<Canvas>....</Canvas>";
    }

    public void ProcessRequest (HttpContext context)
    {
        SPListItemCollection results;
        SPQuery query;
        SPWeb theWeb;
        SPList theList;
        string site;

        site = context.Request["site"];

        if (site == null) site = SPContext.Current.Site.RootWeb.ID.ToString();

        context.Response.ContentType = "text/xml";
        theWeb = SPContext.Current.Site.OpenWeb(new Guid(site));

        query.Query = String.Format("<Where><Eq><FieldRef Name='Department'/><Value Type='Choice'>{0}</Value></Eq></Where>", context.Request["department"]);

        query.ViewFields = "<FieldRef Name='Title'/><FieldRef Name='NTLogin'/><FieldRef Name='Position'/><FieldRef Name='Department'/>";
        results = theList.GetItems(query);
        context.Response.Write(TransformToXaml(results));
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

On the client side we simply instantiate the SilverLight control using javascript and point it to our handler. The handler will run under the security context of the current user and so will only have access to resources on the server for which the user has permissions. In a real world scenario it is likely that you’ll want to pass parameters to the handler in order to vary the data that is returned. This is easily accomplished by passing parameters on the query string.

The overall technique is outlined in the diagram below:

Silverlight interaction with SharePoint

This technique is ideally suited to a host of tasks- not just drawing graphs. For example it would be fairly easy to draw an organisational chart from the SharePoint people directory- but I’ll save that for another post. In fact, I think it would be a great idea if future versions of SharePoint offered a Silverlight interface. Much of the current interface is generated using XML definitions so it would seem like a great way to enhance the useability of the product….

Post a Response