| Subcribe via RSS

Silverlight Host Web Part

October 4th, 2008 Posted in SharePoint, Silverlight

Lot’s of people start developing Silverlight applications these days and I asked myself: Why not host these applications in SharePoint?

I developed a very simple web parts that loads a silverlight application file (.xap) stored within a document library and runs it within its viewspace.

Before you can deploy and use the web part in your SharePoint farm you have to complete the following tasks:

  • 1. Install Silverlight 2 Beta 2 on the server.
  • 2. Copy the file System.Web.Silverlight.dll to the GAC (The file is usually located in C:\Program Files\Microsoft SDKs\Silverlight\v2.0\Libraries\Server)
  • 3. Make neccessary adjustments to the web.config. This step is quite complex so I recommend you watch this screencast from Patrick Tisseghem and follow it step by step.
  • 4. Add this MIME type mapping to the IIS Website: .xap -> application/x-silverlight-2-b2

If you want to recompile the source code of this web part you have to install Visual Studio 2008 and the Silverlight Tools for VS 2008 and the Visual Studio Extensions for SharePoint 1.2.

Download, Deploy and Use

  • 1. Download the wsp file (setup.bat included) and install the web part.
  • 2. Download this sample .xap file that you can show within your silverlight web part.
  • 3. Ensure that the feature “Silverlight 2 Beta 2 Host Web Part Feature” is activated within the site collection.
  • 4. Upload the .xap file into a document library.
  • 5. Edit a page and add the web part “Silverlight 2 Beta 2 Host Web Part” to a web part zone.

  • 6. Now edit the web part properties and set the property “XAP URL” with the url to the xap file.

  • 7. Exit edit mode and refresh the page. The web part should show the silverlight application now.

Source Code

You can download the web part project here.

The first step is to ensure that a script manager exists on the page.

protected override void OnLoad(EventArgs e)
    ScriptManager sm = ScriptManager.GetCurrent(this.Page);
    if (sm == null)
        sm = new ScriptManager();
        Controls.AddAt(0, sm);

The second step is to provide a web part property to consume a URL to the .xap file.

[Personalizable(PersonalizationScope.Shared), Category("Silverlight")]
[FriendlyName("XAP URL")]
[Description("URL to XAP file")]
public string XapUrl
    get { return m_xapUrl; }
    set { m_xapUrl = value; }
protected string m_xapUrl = string.Empty;

The third step is to create a Silverlight control and load the Silverlight application into it:

protected override void CreateChildControls()
        if (!string.IsNullOrEmpty(XapUrl))
            Silverlight ctrl = new Silverlight();
            ctrl.ID = this.ID + "_" + "SilverLightControl";
            ctrl.Source = XapUrl;
            ctrl.Width = new Unit(100, UnitType.Percentage);
            ctrl.Height = new Unit(m_controlHeight, 250);
            Label lbl = new Label();
            lbl.Text = "Provide the url to a .xap file.";
            lbl.ID = this.ID + "_" + "LblMessage";
    catch (Exception ex)
        Label lbl = new Label();
        lbl.Text = string.Format("Error: " + ex.Message);
        lbl.ID = this.ID + "_" + "LblMessage";

Have a nice Silverlight day,

Leave a Reply