Wednesday, March 4, 2009

Silverlight and SharePoint: Getting Started

Get Visual Studio 2008 and download SP1 from

The installer may stick at one part and may take an hour or two, so make sure you leave tons of time to spare.
Get the Microsoft Silverlight Tools for Visual Studio 2008 with SP1 from

Get Expression Blend 2.0 from

Download Expression Blend SP1 from

Create a new Silverlight Application using the Silverlight Application Template in Visual Studio 2008 and Expression Blend 2.0

SharePoint specific instructions:
Create a new Virtual Directory on your SharePoint Application in IIS (c:\inetpub\wwwroot\wss\virtualdirectories\webappfolder\) called ClientBin. Make sure the directory has Read and Run Permissions. This directory is where you will put all your compiled Silverlight files (XAP files).

Check the MIME Types in IIS to see if .XAP and .XAML are included (this is added in Server 2008 automagically, but not necessarily in Server 2003). If these entries are not included add them by using the new button and type in Extension: XAML MIME Type:application/xaml+xml and for the second entry Extension: XAP MIME Type:application/x-silverlight-app. Adding these extensions as MIME Types makes them trusted extensions.

Add the System.Web.Silverlight.dll to the Global Assembly Cache (click on Start and then Run and type in Assembly and hit Enter, you can drag the assembly into this window). Note: Once you install the Silverlight Extensions for Visutal Studio this should be in the c:\program files directory under the SDK.

Add a Safe Control to your Web .Config File by navigating to the IIS folder for your SharePoint Application (c:\inetpub\wwwroot\wss\virtualdirectories\webappfolder\). Open the file in NotePad or another editing program and look for the Tag. Add this tag at the end of the entries:
Copy your XAP File into the ClientBin Directory you created above.

Create a SharePoint Webpart and add a reference to System.Web.UI.SilverlightControls and add a using statement. You should be able to create a new control and reference it in the CreateChildControls like such:
public Silverlight MySilverlightControl;
protected override void CreateChildControls()
MySilverlightControl = new Silverlight();
MySilverlightControl.ID = "slControl";
MySilverlightControl.Source = "/ClientBin/MyXAPFile.xap";

These are pretty much all the steps you need to follow to use a Silverlight Control in a webpart. You need to also remember that you cannot directly reference the SharePoint Object Model in a Silverlight Control. You can use the SharePoint Webservices or create your own webservice. Silverlight renders like a client side javascript application and does not work like an ASP .Net Application. It is more of a presentation layer than a data layer.

Another approach that is a simpler way of surfacing a Silverlight app in a web part page is to host the XAP file in a document library and reference that file in an OBJECT tag in a content editor web part.
Have a look at post at for details.