Thursday, January 10, 2013

General purpose Javascript library

The usage of Javascript in CRM2011 is extensive, and there are many applications for it when extending CRM. This article will show how to create a general purpose library that can be used across multiple solutions in multiple forms.

To see the implemtntaion, go to http://learningcrm2011.blogspot.com/2013/01/create-custom-event-handlers-with-crm.html

Step 1 - Create the library.

  • Navigate to the web resource list in the default solution. Putting it here makes it 
  • Click NEW to add a new web resource.
  • Provide a name for the script, i.e. MyImportantLibrary
  • Set the display name as Generic client jscript
  • Set the description as A collection of useful scripts that can be used in all solutions
  • Specify the Type as Script (JScript)
  • Click the Text Editor button to open the editor for the Javascript. As you can see, the editor is truly beautiful.
  • Insert the text for the script. Note that these are general scripts that can be used anwhere; i.e. they are not specific to any entities or relationships etc. You can put your own functions in here.
function saySomething()
{
   alert('hello world');
}

  • Save and publish

Step 2 - Add a reference to the library

  • Open the form where you want to use it, and begin customizing it by clicking the Customize tab on the ribbon, then click the Form icon on the ribbon to open the form editor. When the form is open, click the Form Properties button:


  • When the form opens, click the Add button under form libraries section. Pick the web resource you have just saved and published, and click OK.This essentially includes this script in the page.

Step 3 - Use the library

So I want the script to execute when the page loads. 
  • Under the Event handlers section, ensure Control and OnLoad Event is selected.
  • Click the Add Button for the on load event for the form:
  • When the form opens, select the library you have just added to the form (myImportantLibrary).
  • Under Function, type the code to execute the function. In this case, it would be this:
    saySomething
  • Important - you cannot pass parameters, neither should you add the parentheses to the call. If you do, the page will crash.

No comments:

Post a Comment