CRM 2011 Silverlight Multiple Pages within a Single xap file

Gayan Perera, 28 January 2011

The ability to use Silverlight in CRM 2011 is a great improvement over ASP.NET ASPX. Especially since Silverlight is supported in all deployment types (On-premise, Partner hosted, Online). Therefore it makes sense to use Silverlight instead of ASP.NET ASPX pages.

One of the biggest challenges we faced was figuring out how to use a single Silverlight Application (single .xap file) but allow CRM to pick and display a specific Silverlight Page (.xaml file).

To achieve this we need to tweak a few things, here is a step by step guide…

1. Create a new Silverlight Application

CRM 2011 Silverlight Multiple Pages within a Single xap file

2. Once the project has been created, add a new Silverlight Page

CRM 2011 Silverlight Multiple Pages within a Single xap file

3. Open up the code behind file for App.xaml (App.xaml.cs) and modify the Application_Startup event. You’ll notice that the default code has been replaced to look at the InitParams of the Silverlight control.

We’ll use this parameter to specify which page to show when the Silverlight control first loads.

CRM 2011 Silverlight Multiple Pages within a Single xap file

4. Now create a blank html file to host this control and copy paste the following into it.

<html>
<body>
    <object data="data:application/x-silverlight" type="application/x-silverlight" width="100%" height="100%">
        <param name="source" value="/WebResources/new_example" />
        <param name="initParams" value="page=SilverlightApplication3.OurCustomPage" />
        <param name="background" value="white" />
        <param name="minRuntimeVersion" value="4.0.50826.0" />
        <param name="autoUpgrade" value="true" />
    </object>
</body>
</html>

The source parameter is important, to get the source url, upload the .xap file, once you’ve uploaded it CRM will give you a url similar to this - https://org.crm.dynamics.com/webresources/resource_name_you_specified. Because we want this web resource to be portable, we’ll take away the hostname and just use /webresources/resource_name.

The initParams parameter, this is where we’ll be specifying our custom key, in the above example we’ll use page and give it the type name of the page we want to load initially.

5. Upload the .xap file and the html file as web resources
6. Add the html web resource to an entity
7. Publish all customizations and give it a try!

You can tweak this to be more generic, for example pass a parameter to the html page from the entity form customization instead of creating an html page per xaml page to avoid duplication of code.

In the next post we’ll deep dive and look at utilizing the CRM 2011 SDK via Silverlight.