Dynamics CRM in the Field

Information from the Microsoft Dynamics CRM PFE team working in the field

Building Multi-Purpose Silverlight Web Resources in Dynamics CRM 2011

Building Multi-Purpose Silverlight Web Resources in Dynamics CRM 2011

Rate This
  • Comments 10

A powerful point of extensibility in Dynamics CRM 2011, is the ability to build a library of re-usable Web Resources of varying content type and declaratively introduce those resources in various parts of the native interface to extend the presentation layer.  The web resource approach to extending the UI offers a compelling alternative to integrating separately-hosted ASP.NET Web Forms/MVC apps that minimizes complications arising in areas such as security/identity impersonation, deployment, and solution distribution (For more details on CRM 2011 Web Resources in CRM SDK on MSDN: http://msdn.microsoft.com/en-us/library/gg309473.aspx). In addition to these benefits, CRM 2011 supports Silverlight (.XAP) as a web resource content type for creating rich interactive user experiences.  I’m pleased to see more Partners and Customers transitioning to leverage this technology to solve sophisticated presentation challenges.

A Trend Identified in CRM 2011 Silverlight Web Resources Development

Nevertheless, while observing this transition, I’ve noticed a trend in development approach where a separate Silverlight Web Resource is created to address each area of the UI needing to be extended within a common CRM solution, even when extending the same general area such as an entity form.  This approach may work for meeting a small set of functional needs, but quickly presents scalability concerns in the form of CRM solution export file size bloat and initial page-load performance.  Underlying contributors to unnecessary solution export file size bloat often include shared dependencies or code such as:

  • Client data services generated classes
  • Shared internal libraries/class files/resources
  • Third-party libraries

Generating the OrganizationData.svc client data service classes for alone can contribute approx. 500kb - 1mb to the overall physical file size of your .XAP output. This can grow significantly beyond that if your solution contains many custom entities. Add in a shared internal library and a third-party controls library to each of your Silverlight applications and you could easily be contributing 2 – 3mb to each .XAP from these application dependencies.  If creating one or two Silverlight Web Resources, this may not present too much of a concern. But, what if you were to introduce 5, 10, or 20 unique extensions? Your solution file size could easily contain 15mb – 60mb of duplicate dependencies in the .XAP Web Resources!

Applying Silverlight Navigation to CRM 2011 Web Resources

A simple approach that I’d advocate for avoiding the situation described above is to consolidate your extensions into fewer Silverlight application projects and employ a navigation pattern to the application.  This approach will essentially allow a single Silverlight application to present and navigate between multiple views similar to pages in a website.  Putting this approach into context with CRM 2011, we would have a single Silverlight web resource, reference it multiple times throughout the UI of our solution, and in doing so specify which view should be presented for each instance.  For our application to determine which view to present, we would register the view name in the InitParams, read this value out when the application is loaded, and navigate to that view.

For smaller solutions or single developers, a single Silverlight application project may suffice. For larger, more sophisticated solution or collaborative development teams, you may choose to consolidate extensions by functional area or entity. Seek to strike a balance between effective consolidation without adversely impacting team collaboration during development/testing.

So, how do we implement this approach?  Here’s a quick overview (we’ll assume you’re familiar with building Silverlight applications and adding CRM 2011 Web Resources) applied to a basic scenario. 

How To Implement the Approach

The scenario is a need to extend the Opportunity form to display customer information, provide an editable products grid, and invoke a custom dialog via Ribbon button command.  We’ll build a single Silverlight application that contains separate views for each of the three extensions.  The MainPage.xaml will essentially be the host container for each of the views and will handle navigating to each (For more details on building navigation applications in Silverlight on MSDN: http://msdn.microsoft.com/en-us/library/cc838245(v=VS.95).aspx).  To begin, the MainPage.xaml UserControl will need to reference the Navigation namespace.

<UserControl x:Class="Microsoft.Pfe.Xrm.SilverlightNavSample.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"

mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">

The navigation Frame control will then be placed in our MainPage.xaml layout root.  An event handler for the Loaded event will parse the InitParams values and perform the navigation.  The UriMapper will convert the value passed in the InitParams to the source Uri for navigation.

<Grid x:Name="LayoutRoot" UseLayoutRounding="True">
<navigation:Frame x:Name="MainContentFrame"
Loaded="MainContentFrame_Loaded"
Style="{StaticResource MainContentFrameStyle}"
UriMapper="{StaticResource NavigationMapping}">
</navigation:Frame>
</Grid>

The UriMapper can either be defined inline or within a resource dictionary.  Above I referenced a static resource and below is the associated XAML from that resource dictionary (For more details on Silverlight resource dictionaries on MSDN: http://msdn.microsoft.com/en-us/library/cc903952(v=VS.95).aspx).  Note that multiple UriMappings can be defined.  Below is a simple example of accepting a user friendly relative path and redirecting that to the intended folder structure for our pages (views).

<!-- Uri Mapping -->
<uriMapper:UriMapper x:Key="NavigationMapping">
<uriMapper:UriMapping Uri="/{webResource}" MappedUri="/Views/{webResource}.xaml"/>
</uriMapper:UriMapper>

The Loaded event needs to be handled in the code-behind for the MainPage.xaml.  Here is a very basic illustration of reading the ‘data’ parameter supplied in the InitParams by an instance of CRM 2011 Silverlight web resource.

private void MainContentFrame_Loaded(object sender, RoutedEventArgs e)
{
if (App.Current.Host.InitParams.ContainsKey("data"))
{
var viewUriString = string.Format("/{0}", App.Current.Host.InitParams["data"].ToString());

MainContentFrame.Navigate(new Uri(viewUriString, UriKind.Relative));
}
}

When creating views for the application, we’ll need to use a Silverlight Page control.  Below is the XAML and namespaces included for this item type.

image

<navigation:Page
 x:Class="Microsoft.Pfe.Xrm.SilverlightNavSample.Views.OpportunityCustomerDetailsView" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"

d:DesignWidth="640" d:DesignHeight="480"
Title="OpportunityCustomerDetailsView Page">

From this point, you would implement the XAML for your view within this Page control and bind your ViewModel to the layout root.  Your Silverlight application project may resemble something similar to below after adding the views, view models, and a reference to the OrganizationData.svc endpoint. 

image

After building your Silverlight project, the .XAP will be available in the /ClientBin folder of your target web application project.  The size of this file is ~1mb without any additional shared/third-party libraries or additional code to flesh out the view models, commands, etc.

image

We’ll instantiate this Web Resource directly on the Opportunity form for each of the two embedded extension scenarios, customer details and editable grid.  For the dialog scenario, add an HTML page to host the Silverlight Web Resource since it will be opened via a Ribbon Button command.  The easiest approach to create the host page is to utilize the contents of the HTML test page generated when creating your Silverlight application project.  Within the Silverlight plugin object reference, we’ll need to specify our data parameter in the InitParams collection (For more details on setting InitParams for the Siliverlight Plugin Object on MSDN: http://msdn.microsoft.com/en-us/library/cc838255%28v=VS.95%29.aspx).  Notice also that the ‘source’ value can be a functioning relative path assuming it matches the name for the Silverlight Web Resource.

<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
<param name="source" value="../ClientBin/Microsoft.Pfe.Xrm.SilverlightNavSample.xap"/>
<param name="onError" value="onSilverlightError" />
<param name="background" value="white" />
<param name="minRuntimeVersion" value="5.0.61118.0" />
<param name="autoUpgrade" value="true" />
<param name="initParams" value="data=OpportunityDialogView" />
        
<a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=5.0.61118.0" style="text-decoration:none">
<img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="Get Microsoft Silverlight" style="border-style:none"/>
</a>
</object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe>

We’re now ready to define and upload our Web Resources to CRM.  For the stated scenario, we’ll create three new Web Resources:

  1. Silverlight XAP:     /ClientBin/Microsoft.Pfe.Xrm.SilverlightNavSample.xap
  2. Web Page HTML:  /Pages/OpportunityDialog.htm
  3. Script Library:        /Scripts/Silverlight.js (this is the Silverlight library created by default in the target web site project)

Note that in the naming of these Web Resources we’ll want to maintain the relative path structure of our web resources project.  This naming convention allows us to easily create references between multiple Web Resources such as an HTML web page embedding a Silverlight application. 

image

On the Opportunity form, we’ll create two instances of the same Silverlight Web Resource and configure the data parameter to specify the appropriate view to navigate to.  Below is an example of a Web Resource instance’s properties as defined on an entity form.

image

To enable a custom Ribbon button to invoke our host dialog page in a modal dialog window, we can add the following Action to the Button’s CommandDefinition. Since we have an HTML page to host our Silverlight application, we can use the Url action type and specify the relative path to the HTML Web Resource.  Notice we’ve added the $webresource: directive which creates a dependency between the Ribbon definition and our HTML Web Resource.

<Actions>
<Url Address="$webresource:new_/Pages/OpportunityDialogPage.htm"
WinMode="1"
WinParams="dialogHeight:300px;dialogWidth:500px;" />
</Actions>

After publishing all customizations, here is what the extended Opportunity form would look like with three individual Web Resource extensions all being presented by a single Silverlight Web Resource!  Our solution size remains tidy and there is no additional download when opening the modal dialog since the application is already cached client-side. 

image

Let me know your thoughts on the above approach.  How do you see this benefiting your CRM 2011 solution?  What challenges/issues do you foresee?  Please provide a comment to discuss. 

P.S. Stay tuned for a future blog post where I explore a more sophisticated approach using Prism patterns to create modularized CRM 2011 Silverlight web resources!

Austin Jones

Microsoft Premier Field Engineer

Attachment: Multi-Purpose Silverlight Web Resource Sample.zip
  • Hi,

    Thanks for the article.

    What's your view on developing silverlight solutions that can eventually grow to a point that is no longer portable?

    Regards

    Nuno

  • When you say 'no longer portable', I assume you refer to potential issues with distributing and/or deploying the solution due to sheer file size?  Sort of goes without saying that such a situation should be avoided.  The above approach intends to mitigate that by consolidating what may be structured as separate applications, thereby minimizing and/or eliminating altogether file size associated with multiple references to components common across the applications.

    If you had another angle to your question, please provide more context and I'd be glad to respond.

  • Its a great post and worthwhile technique - is there an accompanying code sample please?

  • This is an excellent article. I look forward to your views on using Prism with Dynamics CRM 2011.

  • I don't know where you would actually run into this issue in the field though...JK   ;)

  • Great article Austin.  I don't know where you would actually run into this problem in real life though... JK  ;)

  • @Stephen - Just uploaded the source code, although you really have the important stuff called out in the post contents. I had figured the implementation details of each View was irrelavent to illustrating the general pattern.  Let me know if you have any further questions!

  • Hi Austin,

    Thanks very much for the code example.

    Cheers

  • Great Article..

  • Great post and really helpful in my project. Client just loved it and we will look forward to more work by you.

Page 1 of 1 (10 items)
Leave a Comment
  • Please add 8 and 8 and type the answer here:
  • Post