I have recently been prototyping some concept applications for Windows Live using managed code in Silverlight V1.1 Alpha and thought it would be cool, if Dynamics CRM developers could take advantage and write their client side logic in, lets say, C# rather than JavaScript. Currently, you can use CRM form events (Onload/OnSave/OnChange) to add JavaScript code to the forms. Some of the reasons why someone would do that is to create richer and more interactive user experience, implement form verification/validation or mash up data from multiple sources, without spending the cost of making a round trip to the server.
I am excited about Silverlight V1.1 and the ability to write client side code in C#, so I spent a few hours to build a simple example that shows how to extend Dynamics CRM forms business logic using Silverlight V1.1 Alpha managed code. Here is why I think anybody should care about this sample compared to what is already enabled:
- Developer experience: I personally have more fun writing in strongly typed and rich languages like C# than JavaScript.
- Performance: Silverlight runs compiled C# code on the client. JavaScript is an interpreted language and hence generally slower in performance.
- Protecting your investment: There are limited ways to protect JavaScript code since the code is viewable by the client, on the other hand, the prospects of protecting Silverlight managed assemblies is much higher (Obfuscation, MSLP, etc).
Note that all these reasons only makes sense if you are convinced that your code should run on the client and don’t want to pay the cost for making a round trip to the server for processing every function.
On to the sample…..the sample implements a very simple logic just to help better understand the architecture: if the value of a field is changed, based on some logic, set the value of another field.
Requirements
- Install Visual Studio 2008 RTM (install the trial verion for free).
- Install Microsoft Silverlight 1.1 Tools Alpha for Visual Studio 2008 RTM. This tool enables you to create, view and compile Silverlight 1.1 projects in Visual Studio.
- Install Microsoft Silverlight 1.1 Alpha September Refresh Runtime on all the clients that you want to run the managed assembly on. Note that Silverlight will automatically install this on the first use so no deployment is really needed. But to get your development environment quickly setup, I suggest that you install the runtime on your dev/test machine.
- Access to a server that runs Microsoft Dynamics CRM 3.0 or 4.0 (I have tested it with CRM V4.0 RC0)
Get started
1) Build your Silverlight Assembly: First lets build a Silverlight managed assembly to be called by CRM forms. Open VS and create a new Silverlight V1.1 project (or use the one that I have already created in the attachement). VS creates some default files. Rename the files to make your project look like below image (ignore the OnChange.js file for now).
Update the HTML page of the project, CRMSilverlight.html, to look like this. It is pretty standard, all I did was to rename some of the files. This HTML page is the one that is hosting the Silverlight control and will be exposed through an IFrame on CRM form(see below).
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<!-- saved from url=(0014)about:internet -->
<head>
<title>Microsofft CRM Silverlight IFrame</title>
<script type="text/javascript" src="Silverlight.js"></script>
<script type="text/javascript" src="CRMSilverlight.html.js"></script>
<style type="text/css">
.silverlightHost { width: 100%; height: 100%; }
</style>
</head>
<body>
<div id="SilverlightControlHost" class="silverlightHost" >
<script type="text/javascript">
createSilverlight();
</script>
</body>
</html>
Add a new C# managed method to Page.xaml.cs by creating calculateRating method. This is a simple method that will be called from CRM form. This is where all the fun begins, you can write all of your client side code in C#, beautiful! The method takes an int from CRM form and returns a string reflecting how good the credit score is, simple. The Scriptable tags simply means that the class and the method are accessible from JavaScript.
namespace CRMSilverlight
{
[Scriptable]
public partial class Page : Canvas
{
//This method is Scriptable which means it can be called from HTML DOM
//Put all of your complex logic in this method. You can write all of your logic in C#
//This methods takes a credit score integer and returnes a string rating
[Scriptable]
public string calculateRating (int creditScore)
{
string creditRating;
if (creditScore <= 500)
creditRating = "Bad";
else if (creditScore >500 && creditScore <=700)
creditRating = "Good";
else
creditRating = "Excellent";
return creditRating;
}
//When the page is loaded, a new object called basic is registered
//This object is used to access methods and properties of this class
//from HTML DOM
public void FromCRMForm(object o, EventArgs e)
{
// Required to initialize variables
InitializeComponent();
// This class will be referenced in JavaScript using keyword "basic"
WebApplication.Current.RegisterScriptableObject("basic", this);
}
}
That is it! compile your project. It should compile with no errors and create two files in the ClientBin folder of your VS project: CRMSilverlight.dll and CRMSilverlight.pdb. You are now ready to copy the project files onto the CRM server.
2) Publish the Silverlight managed assembly on CRM server
Now you need to put the project files on IIS so they can be accessed by CRM forms. Go to the server where CRM server is installed. Open IIS manager and create a new folder, SLCRM, in CRM web application root. Copy the files that are shown below from the VS project into this folder, including the ClientBin folder and its content (it includes the Silverlight managed assembly, CRMSilverlight.dll and its pdb, CRMSilverlight.pdb). Make sure that all the files, including the dll, can be opened from your browser (i.e. IIS correctly serves all the files).
3) Add an IFrame and two custom fields to CRM Form
Choose a CRM form to host the Silverlight control. I used the Account form and added an IFrame to the bottom of the Administration tab. Note that this IFrame is only there to host the Silverlight control on the page so you can make it as small and unnoticeable as possible. Set the URL of the IFrame to the URL of the CRMSilverlight.html file that you published on the server in step 2.
Now add two attributes to the Account Business Entity: Silver_CreditScore (number) and Silver_CreditRating (string). The attributes will then show on CRM Form designer as fields. Add these fields to the Account form, Administration section. All standard CRM customization stuff.
4) Hook up CRM form events to call the Silverlight managed assembly
In CRM form designer, add the following JavaScript code to the OnChange event for Silver_CreditScore field that you earlier added to Account form. Whenever this field is changed, this event is fired.
//This is how to access the Silverlight control on the IFrame page
//IFRAME_HiddenSL is the ID of the IFrame added earlier
var control = window.frames['IFRAME_HiddenSL'].document.getElementById('SilverlightControl');
var score = crmForm.all.silver_creditscore.DataValue;
//This is how to access the calculateRating method in the Silverlight managed assembly
var rating = control.Content.basic.calculateRating(parseInt(score));
crmForm.all.silver_creditrating.DataValue = rating;
I also included this script in the VS project that is attached (in OnChange.Js file).
That is it folks. Now save and publish the Account form using CRM customization tools. Open an account form, this will load the Iframe that has the Silverlight Control on it. Set a value in Credit Score field, this will fire the OnChange event. The OnChange event will then reach into the Silverlight Control in the IFrame and call the C# calculateRating method by passing the value of the Credit Score field. The rating gets calculated in managed assembly that is running in the browser and return the result to the CRM form. The last line of the OnChange script sets the value returned from Silverlight managed assembly to the Credit Rating field. Sweet...
The VS project for this sample is here:
CRMSilverlight.zip (131.88 kb)
Currently rated 4.6 by 9 people
- Currently 4.555555/5 Stars.
- 1
- 2
- 3
- 4
- 5
Dynamics CRM has an offline client (implemented as an Outlook add-in) that allows you to manipulate CRM data while offline and automatically synchronizes that data with the CRM server when you go back online. In Dynamics CRM 4.0, we opened up our offline client APIs for developers to build offline applications and as I have said over and over, Dynamics CRM is not about CRM applications only, one can build different types of applications on top of this platform that is currently called Dynamics CRM. When Dynamics CRM Live service and offline client APIs are used together, it enables very powerful applications that seamlessly work online and offline (and occasionally connected).
I will write more about CRM 4.0 offline APIs and programming model later on but for now, CRM aside, I have a request:
Are you a web site owner who wants to enable your customers to interact with your content while offline and get the updates automatically synchronized with your service? if so, what challenges have you faced? How useful did you find the existing platforms and tools? What is missing from today’s mix that prevents you from building client-cloud (Software and Service) enabled solutions. Do you even think such solutions are interesting?
Drop me a line, I’d love to hear from you.
Currently rated 5.0 by 1 people
- Currently 5/5 Stars.
- 1
- 2
- 3
- 4
- 5
One of the biggest feature requests that I have heard from ISVs, over and over, is the ability to protect their Intellectual Properties (IP). For example, Dynamics CRM ISVs typically implement the custom business logic of their application as a callout managed library(dll) that plugs into CRM execution pipeline. These dlls are part of the ISV solution and should be protected against reverse engineering and illegal copying. Often these solutions are sent to customers for evaluation and after a period, customers may decide not to make a final purchase or stop renewing the ISV license.
The problem is that although ISVs are legally protected by their End-User Licensing Agreement (EULA) against such cases, in practice, most ISVs are not wealthy enough to bring a litigation case against every such customer. So what is the solution? Shouldn't we have a software solution to this problem instead of relying on legal documents?
Here is one possible solution: Microsoft Software Licensing and Protection Services (SLPS). SLPS uses a selective .NET code transformation technology that help ISVs protect their IP with a higher level of protection. There are already some other solutions out there but I highly encourage you to check out SLPs and see how different it is. One caveat is that CRM solutions have more than just dlls. They have xml files that contain customization and client side scripts. What about those non-.NET files? Technically SLPS will not help there a lot and since CRM platform reads and processes these xml files, one place to put a gate would be in the CRM platform itself…..
I’d love to hear your thoughts, especially from the folks who have tried SLPS with Dynamics CRM. Did you manage to get it to work? what was your experience?
Currently rated 2.0 by 1 people
- Currently 2/5 Stars.
- 1
- 2
- 3
- 4
- 5
Most of Microsoft CRM partners are already aware of what is in store for Dynamics CRM 4.0 and more details are emerging about the technologies that you will see in this release. One of the areas that I had worked on (wrote pages and pages of functional specification and design) for the past 1-2 years while being a member of Dynamics CRM team, is integration with Windows Workflow Foundation(WF). You could integrate CRM 3.0 with WF but you are in for a big surprise to see the power of CRM 4.0, when comes to workflow and WF integration (more on this later...).
To get you prepared, I’d like to recommend an excellent book on WF. Dharma, who is a close colleague and one of pioneers of WF at Microsoft, has co-authored the Essential Windows Workflow Foundation (Microsoft .NET Development Series) book which I highly recommend, because 1) it is a great book 2) it is written by a great guy 3) if you want to be a highly paid and valued CRM developer, you must understand the concepts that are described in this book....go get it today.....
Currently rated 4.0 by 4 people
- Currently 4/5 Stars.
- 1
- 2
- 3
- 4
- 5
Oct 29 - Nov 2 | Redmond, WA
I have seen some of the content and highly recommend this conference. If you are an enterprise developer wanting to learn about the latest and greatest in Business Process and SOA, this is the conference to be at. What I like about it is its focus. You don't see a lot of diverse and unrelated topics unlike when you go to TechEd, for example. Best opportunity to drill down and network with the field experts to learn about our investment in this space and upcomming technology. Also, if you are a CRM ISV or VAR, the conference will give you a lot of background about why we made the decisions that we made in Titan, especially around workflow and Web services.
Be the first to rate this post
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5
Visual Studio 2008 (Orcas) has many new and exciting features for developers(Enhanced .NET Data access and IntelliSense for JavaScript are so far my favorites) and it brings nothing but good news for Microsoft CRM developers. I can clearly see multiple ways that CRM developers could benefit from the new Orcas tools. If time permits, I will write about them at some point but in the meantime, one feature that struck me a few weeks back is LINQ to XML. This feature allows you to write SQL like .NET code (LINQ-style) to interact with any XML document (in memory or on file).
As the Program Manager who wrote the specification for Microsoft CRM QueryExpression (a .NET based query language that returns strongly typed Business Entities) and was responsible for FetchXML(a XML based query language), I know that writing SQL-like queries that return strongly typed data would be appreciated by CRM developers. Simply because, most of the CRM developers are already familiar with SQL.
It shouldn’t take you long to use LINQ to XML and CRM FetchXML to build a Query helper code that allows you write SQL-Like Select statements against CRM data and retrieve strongly Business Entities. Scott’s blog has good descriptive data about where to start. Give it a try, it would be fun….
Be the first to rate this post
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5
As Web 2.0 popularity increases and mash-up applications become more and more popular, cross-domain data access issues on web applications and platform becomes more and more interesting to solve. The problem that exists today with web applications is that any script that you add to a web page can only call into an endpoint hosted on the same domain as the page is served from. So if you want to build a gadget that is served from, lets say live.com, the browser will prevent you from calling into a web service end point on , lets say, mySite.com. This is a security feature that is put in the browsers a long time and is valid but there are new valid scenarios, as I mentioned (mash-ups), that you do want to do this in a secure way. The same problem exists in Microsoft CRM V3.0. If you want to add some Jscript to a CRM form to call a web service hosted on a different domain other than the one that the CRM page is being served from, your browser will prevent you from making the call. Of course there are (very bad) workarounds like lowering your IE security level which is a bad bad thing to do. A good alternative is to create a proxy on the server that is serving the web page to tunnel through the calls to other sites on the server side. Live.com gadgets use such a model.
One interesting approach that I recently learned about is what Danny Thorpe, my colleague at Windows Live Developer Platform, have suggested in the recent issue of the Microsoft Architecture journal. Danny does a good job describing the problem and provides some suggestions that allows cross-domain data access in a secure way. I should also mention that this issue of the architecture journal focuses on web architectures and there are a number of other cool articles in there that you may find interesting.
Download the latest issue of the journal here.
Currently rated 4.0 by 1 people
- Currently 4/5 Stars.
- 1
- 2
- 3
- 4
- 5