Friday, April 27, 2012

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.

No comments: