Friday, April 27, 2012

Server Roles in Dynamics CRM 2011

One of the questions which always confused me about CRM was about how to scale a CRM installation. So I decided to put my reading hat on and here’s what I figured out

Dynamics CRM divides the installations into 3 Server Role Groups. These Role Groups are further divided into Server Roles. When you are deploying a Dynamics CRM installation you can decide to install various roles on different machines or group of machines to ensure that you meet your scalability requirements.

1) Front End Server Role Group :-

This is the group of server roles which renders the CRM UI and enables various external applications to connect to CRM using CRM SDK or web services. This roles in this group are :

a) Discovery Web Service :- Contains all the components required to host and run the Discovery Web Service. This is a Deployment scope web service which serves the whole deployment i.e. if you have N organizations in the CRM you will still have a single instance of the Discovery Service running.

b) Organization Web Service :- Contains all the components required to host and run Organization Web Service. This is also a deployment scope web service. So as you create more and more organizations and more and more applications consuming this web service you might want to scale this role.

c) Web Application Server :- Contains all the components required to render CRM data to the end users. This is also a deployment scope role. As the number of users on CRM increase you want to scale this role.

d) Help Server :- Contains all the components required to make CRM Help to the end users. This is a deployment scope role

2) Back End Server Role Group :-

This is a group of server roles which are not exposed on internet or to the end users. These roles are contacted by the Application roles to delegate the backend processing like workflows, custom plug ins. The roles in this group are :

a) Asynchronous Service :- Processes the async events like workflows, plugins, data import jobs etc. This is a deployment scope role. As you have more and more async events you want to scale this role.

b) Sandbox Service :- Processes the async sandboxed plug in events. This provides isolation to the plug in code. This is a deployment scope role. As the number of sandboxes plug in events increase in your deployment you want to scale this role.

3) Deployment Administration Role :-

This includes the server roles for components that are used to manage the CRM deployment. The server roles in this group are

a) Deployment Web Service :- Allows to manage the deployment. You can scale this out as you have more and more organizations in your deployment. This is a deployment scope service.

b) Deployment Tools :- Consist of Deployment MMC and Windows Powershell cmdlets. You can install these tools on your Admin boxes. This is a deployment scope service.

4) SQL Server :- This is not really a role but you need a SQL Server cluster which you point to for storing the deployment configuration and organization data.

Same information is demonstrated visually


Hope this helps in understanding the components with more clarity and plan your deployment such that you can scale easily.



Intellisense in Visual Studio 2010 for CRM 2011 Client Side Scripting

Inteliisense is one of the features I can’t live without. Since JavaScript Intellisense was not available for CRM Object Model I always struggled with it. With CRM 2011 release Microsoft has made life a bit easy by providing a solution to enable intellisense on the CRM Object Model. Here’s how you can enable it in your development environment

1) Install the visual studio JScript editor extension from here.
2) Install the extension XrmPageScriptDevelopmentProjectCS.vsix which comes with CRM SDK. This is available in \sdk\templates\xrm.pagescriptprojecttemplate folder in your SDK install location. You need to install xrmpagescriptdevelopmentprojectvb.vsix if VB.NET is your language of choice.
3) Import the as a solution into your CRM development box.  You can find this file in the same folder where you found the vsix file i.e. \sdk\templates\xrm.pagescriptprojecttemplate
4) Once you have done this create a new project using the template you installed in step 2.
5) This will create a project template for you. The template will look like the following in your solution explorer

6) The files XrmPageTemplate.js and PageData.js are the files which are responsible to provide you the IntelliSense in Visual Studio. While the files My.Library.js and My_Library.js are the ones where you can write your own code.
7 )  But before you try to write code you need to do a little trick to have the IntelliSense working correctly. Let’s say that you want to write java script for the Accounts form. Go to CRM UI and open the form for which you want to write script. You will see another button in the customize ribbon. Like the one shown below (Xrm.Page.Snapshot)
8) Click on this button and you’d see a dialog box like the following. Select event as None and click “Get Data”
9) Copy the text in the textbox and paste it into PageData.js file in the project you had created in step 4. Save the file.
10) You are all set to write a javascript function which can be called on the Account Form now. Let’s write a function fire onChange event of name attribute on the account form.
Start typing and intellisense should support you as shown in the screenshot below.
11) Note that you won’t get the Intellisense when you try to type the name of the attribute. But if you type it wrong you won’t get page specific intellisense on the nameAttribute variable you typed. See the screenshot below to find what happens if you type the wrong name.
12) If you type the right name you get proper intellisense which tells you what you could do with this attribute e.g. fire an onChange event for this attribute as shown below

13)One of the limitations of above solution is that you can’t use the intellisense on eventArgs i.e. if you want to work within the context of an event. In order to enable that you need to repeat steps 7 through 9 and select the appropriate even in the dialog box which pops up in step 8.

   There are some limitations to this solution. E.g. the intellisense support will stop if you try to call a chain of functions. It will stop working when you try to do a Boolean calculation within an if statement as well. There might be more. So trust Intellisense to some extent and there’s still no way to say that if Intellisense doesn’t show it then it’s not there J.
You can also do some amount of testing based on this project. I’ll keep it reserved for another post.

Thursday, April 26, 2012

CRM PlugIn Vs. Client Side Scripting

This is one of the questions which always bothers a CRM developer. If there's a piece of functionality which needs to be developed should I develop it on the client side using JavaScript or should I use a PlugIn to develop it. The answer is it depends :). 

The thumb rule I have is that anything which has business logic or a business rule validation definitely needs to be present in a PlugIn. This ensures that your application is safe independent of the UI which the end user is using.

So what should client side scripting be used for. I believe it should be used for any CRM UI customizations and any validations which you want to perform at the client side.

Validations should avoid the server round trips for performance reasons. So if a validation depends on some data which needs to be pulled from server then I suggest let the PlugIn take care of it. The limitation in this case is that the error box is not very pretty. If you believe that this is not acceptable from the User experience perspective then you should consider embedding some virtual fields in the entity PropertyBag in the Retrieve PlugIn. Pull those dependent fields on the form when the entity is retrieved and then do the validation on the client side.

What if the validation needs to be based on the latest values of fields in another entity? I’ve never come across such a requirement and frankly in web world nothing is really real time. You’ve the server side PlugIn which will redo the validation anyways and if there’s something wrong with the latest value it will throw an error to the user.

So to summarize
1)  Any business logic should always be written in the plug in
2)  Any validation should always be written in a plug in
3)  Any validation that you want to perform on CRM UI should use the javascript API of CRM
4)  Avoid the validations which need a server round trip. Do them only if someone puts a gun on your head J



External config for CRM Online wsdl based proxy

The sample which comes with CRM SDK to connects to CRM Online services uses an extension method and a partial class to attach the appropriate federation tokens for authenticating the calls against live services. 
The extension method is given for any class which derives from ClientBase. The DiscoveryServiceClient and OrganizationServiceClient are the proxy classes which derive from ClientBase and ClientBase respectively. The limitation of deriving from ClientBase is that the WCF Configuration has to be read from app.config or web.config files. 
Sometimes there are limitations where you want to read these configurations from an external file e.g. if you are shipping an add-in to outlook you definitely don't want to ship outlook.exe.config file. 
.NET 4.5 adds the capabilities for reading the configuration from external file but if you are developing against .NET 4.0 or earlier versions you face an issue and the only way out is to use a Custom Channel Factory as described on 

And instantiate the Channel using the Custom Channel Factory. However this class doesn't have a method to configure federation and attach appropriate tokens to the channel to call the CRM Online service successfully. 
The attached sample provides an additional method in this class which can be used to attach proper tokens to the channel. 

The sample can be downloaded from here

Happy coding!!!