D'Arcy from Winnipeg
Solution Architecture, Business & Entrepreneurship, Microsoft, and Adoption

SharePoint and Silverlight – Displaying Silverlight in a Page

Wednesday, May 13, 2009 1:32 AM

I had the pleasure today of trying to get Silverlight and SharePoint to play nice with each other. You’d *think* this wouldn’t be that difficult. But as I’m learning more about SharePoint, I’m realizing also that things are sometimes more complex than we assume.

Prelude: Clarification on Form Content Web Part

So in a previous post, I talked about my frustration with how code in a Form Content web part wasn’t behaving the way I expected. Instead of showing the Silverlight app it was always showing the PluginNotInstalled template message. What I was missing (and wrongly assumed) was that the Form Content wp didn’t mean an ASP.NET WEBform but an HTML Form. So while I was putting in <asp:… tags, they were just being output as if they were text…and ignored by the browser. So what I thought would be an easy way to display Silverlight ended up…well, not.

So let’s look at what you need to do to get Silverlight hosted within a page in SharePoint. My scenario is pretty simple: I have a page (utilizing a layout) that I want to simply display a Silverlight application in. I want to show a custom message if Silverlight isn’t installed so I’m going to take advantage of the ASP.NET Silverlight control. This control has a PluginNotInstalled template that you can put whatever markup you want in there to display when a user hasn’t installed the plugin yet.

Step 1: Prep The Environment

Karine Bosch has a great series of blog posts that describe what you need to do for getting Silverlight hosted within a SharePoint page. Step one is to prep your environment. She lists on her blog the pre-requisites that need to be installed either on your development and deployment environments.

She also has a link to a great article by Jan Tielens that shows a shortcut for upgrading your SharePoint web.config file to take advantage of .NET 3.5 features. Definitely take advantage of this!

Once your web.config file has been updated, you need to drop the System.Web.Silverlight.dll into the GAC, add the Silverlight MIME types to IIS, and add a node in web.config under system.web/compilation/assemblies for the Silverlight dll (read Karine’s article, its all in there).

Step 2: Prep the Page

Another of Karine’s articles walks us through the next stage, although I did find a few steps could be left out and one that needed to be added. From her article:

In your web.config you have to add an extra tag for the silverlight control in the <system.web><pages><controls> section. This section must at least contain the following:

    <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

    <add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

    <add tagPrefix="asp" namespace="System.Web.UI.SilverlightControls" assembly="System.Web.Silverlight, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />

Now after this, she talks about the page and having to specify the @Assembly declarations and importing the namespaces. I didn’t actually have to do that in my page for Silverlight to work. The only thing I can think is that Jan’s trick (mentioned above) took care of moving those declarations to the web.config for all pages so declaring it on each page isn’t necessary anymore.

I did have to add Silverlight and the Web Extensions assemblies to the SafeControl node collection, otherwise I’d get an error message saying that the control couldn’t be rendered because it wasn’t safe.

<SafeControl Assembly="System.Web.Silverlight, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Web.UI.SilverlightControls" TypeName="*" Safe="True" />

<SafeControl Assembly="System.Web.Extensions, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35" Namespace="System.Web.UI" TypeName="*" Safe="True" />

You need the first one for the Silverlight control, and the second one so that you can use the ScriptManager required for the Silverlight control.

Step 3: View Your Page

Once you have all this set up, you can finally try and test your page. There are different ideas on where a xap file can live with regard to hosting in SharePoint:

- Client Bin folder
- Document Library
- Alongside your page

Whichever you choose, just update the Silverlight control with its location and voila: Silverlight displaying in SharePoint!

In Closing…

There was a lot of trial and error in figuring all this out, especially with trying to determine what nodes needed to be added to the web.config file as safe controls, what was required on the page for import and reference statements, etc. Once you get the template down though, its very easy to re-produce. Realize that there are other ways to host Silverlight within SharePoint:

- Create a web-part to either host the Silverlight app or make a generic web part that can have a location of a xap file passed to it.

- Use the Content or Form web parts and use an Object tag to display the Silverlight.

As I work to finalize this piece of the project I’ll post any other nuances I find, but let me know your thoughts and if you’ve had experiences with installing Silverlight within SharePoint.



No comments posted yet.

Post a comment