Sharepoint webpart for Silverlight 2.0

Before the last release of Silverlight 2.0 beta 1 embedding a silverlight application in a webpart was a somewhat tedious thing to do. Basically, you had to deploy your silverlight.dll and .xaml file within your Sharepoint solution as well as a bunch of javascript files that are called from your sharepoint webpart to create your silverlight content.

With the last release of Silverlight 2.0 all this steps are no longer necessary and we can easily host Silverlight Applications in Sharepoint Webparts just by including Silverlight controls and deploying .xap files with our solution.

For this example, I’ll create a generic webpart that will be able to load any .xap file accessible at our server. It will have a XapUrl property where we will be able to write a Url where the webpart will search for the xap file. Obviously this means that the .xap file must be deployed in our server, I usually do that by including the file in my Sharepoint Solution and deploying it in the _layouts folder of my server.

So following, is the code of my Silverlight Webpart

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.WebPartPages;
using System.Web.UI.WebControls.WebParts;
using System.Runtime.InteropServices;
using System.Web.UI;

namespace Examples.SilverLight
    public class SilverLightWebpart : System.Web.UI.WebControls.WebParts.WebPart
        private ScriptManager _scriptHandler;
        private System.Web.UI.SilverlightControls.Silverlight _silverlightControl;

        private string _xapUrl = string.Empty;
        [WebBrowsable(true), Personalizable(true)]
        public string XapUrl
            get { return _xapUrl; }
            set { _xapUrl = value; }

        protected override void OnInit(EventArgs e)

            _scriptHandler = ScriptManager.GetCurrent(this.Page);
            if (_scriptHandler == null)
                _scriptHandler = new ScriptManager();
                _scriptHandler.ID = "ScriptManager1";

            if (!string.IsNullOrEmpty(_xapUrl))
                _silverlightControl = new System.Web.UI.SilverlightControls.Silverlight();
                _silverlightControl.ID = "Xaml1";
                _silverlightControl.Source = _xapUrl;
                _silverlightControl.Version = "2.0";


Basically what we are doing is adding a ScriptManager and a Silverlight controls to our class. The only things we have to take care of are to ensure that there is only one instance of a ScriptManager in our current page (we do that with the call ScriptManager.GetCurrent(this.Page);) and that we must create the controls in the OnInit event of our webpart instead of the usual CreateChildControls event if not, we will get an exception from ScriptManager.RegisterScriptControl telling “Script controls may not be registered before PreRender

And that’s all, see how simple is now to host silverlight applications in our sharepoint server !


10 Responses to Sharepoint webpart for Silverlight 2.0

  1. Jorge Carvalho says:


    First I would like to thank you for publishing this code.

    I’ve installed all Silverlight stuff on our test server, and already managed to compile, register the SilverLightWebPart on the GAC, and add it to the Site Collection Web Part gallery.

    However, I’m getting an error when trying to add the the webPart on the site, and when I try to preview the webpart in the gallery I get the error below.

    Could you give me some pointers on the install of this webpart?

    Jorge Carvalho

    Cannot import SilverLightWebPart Web Part. at Microsoft.SharePoint.WebPartPages.WebPartImporter.CreateWebPart(Boolean clearConnections)
    at Microsoft.SharePoint.WebPartPages.WebPartImporter.Import(SPWebPartManager manager, XmlReader reader, Boolean clearConnections, Uri webPartPageUri, SPWeb spWeb)
    at Microsoft.SharePoint.WebPartPages.WebPartPreview.get_WebPart()
    at Microsoft.SharePoint.WebPartPages.WebPartPreview.CreateChildControls()
    at System.Web.UI.Control.EnsureChildControls()
    at System.Web.UI.Control.PreRenderRecursiveInternal()
    at System.Web.UI.Control.PreRenderRecursiveInternal()
    at System.Web.UI.Control.PreRenderRecursiveInternal()
    at System.Web.UI.Control.PreRenderRecursiveInternal()
    at System.Web.UI.Control.PreRenderRecursiveInternal()
    at System.Web.UI.Control.PreRenderRecursiveInternal()
    at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

  2. oricode says:


    Without further info is almost impossible to know where is the error. Could you check the system event log to see if there is any clue on what’s happening?

  3. Mike Dent says:

    Hey thanks for the post, the best one about the topic I’ve found – however, I’ve done the code like above and deployed the web part fine, however the silverlight app is just not displaying. If I do a quick write to the page, I can see this (and non silverlight controls I add). I have added the xap file to a document library and used the full URL to the document as the silverlight source property – any ideas why it’s not showing up?!

    Thanks in advance.

  4. oricode says:

    Without further info, is complicated to see what’s going wrong. Maybe it has something to do with locating the .xap file in a doc lib and using the full URL. I assume it’s in the same web application. Could you try to use a relative URL instead ?

  5. Mike Dent says:

    Ok i might seem a bit simple here but where would I put the .xap file and what would the relative URL be?! What is it relative to? Still not got my head around how the SharePoint physical paths work. My DLL is in the bin folder of the respective web app.

  6. oricode says:

    You could deploy it at the _layout folder of the web server which is c:\program files\common files\Microsoft Shared\web server extensions\12\template\layouts. Then you can simply reference it with the relative path: _layouts/your_file.xap

    Having said that, the document library approach should work as well. Just try to put a relative to the xap file and it should work.

    The dll is well located at the bin folder of your webapp

  7. Mike Dent says:

    Ok sorry to clog up your blog, just in case anyone else is having the problem I think I was using a different version of the silverlight runtime on my development server to the Sharepoint server I was deploying it to! Also, I think I needed service pack 1 installed onto the MOSS 2007 server. I eventually put the .xap file in my layouts folder which worked, thanks for all your help.

  8. Hi, first thank you for this source code, the best explanation on the web!

    I have a difference with you, the compiler tells me that the ‘Version’ property doesn’t exist in the Silverlight class. And when I check the generated code, the type of the object is “application/x-silverlight” and i think i need “application/x-silverlight-2-b2”.

    And even more, there is no parameter to the object, and the iframe isn’t present etc.

    So maybe i missed something. But I don’t know!

    Thanks for any help.


  9. Moon says:

    Thank you so much for the post, i tried it and now it works just fine .

  10. mswin says:

    I had some issue with this code on my site.
    when I deploy my webpart to sharepoint the silverlight control is not getting rendered on Sharepoint site page. I have my .xap uploaded to a document libary in my sharepoint site
    I had MOSS with SP1 and Silverlight2Beta2 (version 2.0.30523.8) installed on my MOSS machine.
    I have also added MIME type for .xap( application/x-silverlight-app) in the IIS.

    Anyone , having steps to make this work for sharepoint site please update here.

    Thanks in Advance.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: