I recently read an article on how to build advanced custom CRM reports using ASP.NET and Silverlight. Since it was using Visual Studio 2008 and Silverlight 2, I decided to write a more updated version using Visual Web Developer 2010 Express and Silverlight 4. Also I decided to make the report work with the on-premise version of CRM 4.0 and as you will see it looks very similar to one of the dashboards enabled for the on-demand version of CRM 4.0.
First you will need to make sure you have the necessary tools installed to build a Silverlight application. I decided to go with Visual Web Developer 2010 Express because it is free and Silverlight 4 Developer tools for VS.NET 2010. You will also need to download VisiFire Once you have your development environment setup you can follow these steps to easily create a new dashboard for your on-premise CRM instance.

Figure 1
Step 1: Create a Silverlight 4 Project.
Open up Visual Web Developer 2010 Express and select New Project from the Start page or File menu. Expand the Visual C# project templates and select Silverlight. From the Silverlight project types, choose Silverlight Application (see Figure 1). Name your project CrmSalesPipelineDashboard and click OK.

Figure 2
Now you will see a popup which allows you to choose how to host your Silverlight application. Choose to host the Silverlight application in a new Website with type ASP.NET Web Application Project (which should be the default). Choose Silverlight 4 and check the Enable WCF RIA Services option (see Figure 2). After you click OK it will create your two new projects (CrmSalesPipelineDashboard and CrmSalesPipelineDashboard.Web) in one solution.
Step 2: Add an ASP.NET Entity Data Model

Figure 3
Add a new Models folder to the CrmSalesPipelineDashBoard.Web project where you can store your data models. Right click on the new Models folder and select Add New Item which will open a dialog box. Select the Data section under Visual C# and select the ADO.NET Entity Data Model. Change the name to CrmModel.edmx and click Add (see Figure 3).

Figure 5

Figure 4
Now you will see the Entity Data Model Wizard popup dialog. Select Generate from database and click Next (see Figure 4). Either create a new Connection to your CRM database or choose an existing one from the dropdown list. After you click Next, select the StringMap table from the list of Tables. Then select the Opportunity view from the list of Views. Leave the default options checked and change the Model Namespace name to CrmModel and click Finish (see Figure 5). The new model window will be displayed with the new StringMap and Opportunity entities.
Build the solution to make sure everything is still in order.
Step 3: Add a Domain Service Class

Figure 6
Add a new Services folder to the CrmSalesPipelineDashboard.Web project where you can store your services. Right click on the new Services folder and select Add New Item which will open a dialog box. Select the Web section under Visual C# and select the Domain Service Class. Change the name to CrmDomainService.cs and click Add (see Figure 6).

Figure 7
Now you will see the Add New Domain Service Class popup dialog. Check the checkbox by the Opportunity and StringMap entities, uncheck the Generate associated classes for metadata and click OK (see Figure 7).
Insert the GetSalesPipeline method (see Listing 1) after the GetStringMaps method in the CrmDomainService class.
Listing 1
//Return the total estimated value for each sales pipeline step
public IQueryable<SalesPipeline> GetSalesPipeline()
{
IQueryable<SalesPipeline> salesPipeline = from opportunity in this.GetOpportunities()
join stringMaps in this.GetStringMaps() on opportunity.StatusCode equals stringMaps.AttributeValue
where stringMaps.AttributeName == "statuscode" && stringMaps.ObjectTypeCode == 3
group new { opportunity, stringMaps } by stringMaps.Value into g
where g.Sum(o => o.opportunity.EstimatedValue).HasValue && g.Sum(o => o.opportunity.EstimatedValue).Value > 0
select new SalesPipeline { SalesPipelineStepName = g.Key, TotalEstimatedValue = g.Sum(o => o.opportunity.EstimatedValue).Value };
return salesPipeline;
}
Insert the SalesPipeline class (see Listing 2) after the CrmDomainService class.
Listing 2
public class SalesPipeline
{
[Key]
public string SalesPipelineStepName { get; set; }
public decimal TotalEstimatedValue { get; set; }
}
Build the solution to make sure everything is still in order.
Step 4: Add the Pie Chart to Silverlight
Download the latest version of VisiFire (http://www.visifire.com) and unzip it to C:\VisiFire. Add a references to the both the FJ.Core.dll and SL.Visifire.Charts.dll to the CrmSalesPipelineDashboard project.
Insert the XAML necessary to display the pie chart on the page (see Listing 3).
Listing 3
<vc:Chart Name="salesPipelineChart" xmlns:vc="clr-namespace:Visifire.Charts;assembly=SLVisifire.Charts" View3D="True" Theme="Theme1" Width="800" Height="400">
<vc:Chart.Titles>
<vc:Title Text="Sales Pipeline" FontFamily="Verdana" FontSize="20" FontWeight="Bold" />
</vc:Chart.Titles>
<vc:Chart.Series>
<vc:DataSeries RenderAs="Pie" DataSource="{Binding Data, ElementName=salesPipelineDomainDataSource}">
<vc:DataSeries.DataMappings>
<vc:DataMapping MemberName="AxisXLabel" Path="SalesPipelineStepName" />
<vc:DataMapping MemberName="YValue" Path="TotalEstimatedValue" />
</vc:DataSeries.DataMappings>
</vc:DataSeries>
</vc:Chart.Series>
</vc:Chart>
<riaControls:DomainDataSource Name="salesPipelineDomainDataSource" xmlns:riaControls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.DomainServices" xmlns:my="clr-namespace:CrmSalesPipelineDashboard.Web.Services" AutoLoad="True" d:DesignData="{d:DesignInstance my:SalesPipeline, CreateList=true}" QueryName="GetSalesPipelineQuery" Width="0" Height="0">
<riaControls:DomainDataSource.DomainContext>
<my:CrmDomainContext />
</riaControls:DomainDataSource.DomainContext>
</riaControls:DomainDataSource>
Build the solution to make sure everything is still in order.

Finished Pie Chart
Step 5: View the Sales Pipeline Pie Chart
Run the solution by pressing F5 and view the pie chart in the browser. You will notice that out-of-the-box VisiFire allows you to click on the different pieces of the pie to pull them out.
Other Resources
If you are interested in learning more about .NET RIA Services and how to use them in Silverlight I found these videos helpful:
http://www.silverlight.net/learn/videos/all/net-ria-services-intro/
http://www.silverlight.net/learn/videos/all/ria-services-support-visual-studio-2010/
Like this:
Like Loading...