Tuesday, July 12, 2011

Web Resources in Microsoft Dynamics CRM 2011

Web Resources in Microsoft Dynamics CRM 2011

In earlier version of Microsoft Dynamics CRM, customizers have been able to add Silverlight applications, tie in HTML documents and so on simply by using IFRAMES. So, why did we need an improvement to this in Microsoft Dynamics CRM 2011? Those custom pages were usually hosted in the same web server in which CRM was running or in other web servers. This alone poses a lot of challenges because we had to take care of deploying our applications and keeping them in synch across multiple Web Front ends/ client (e.g. if they wanted their applications to run in the CRM offline client they would have to manually install their apps in each and every client machine).

As much as developing a slick web application is a lot of fun…it is not exactly scalable in the typical scenario. If that was not enough, those customers running Microsoft Dynamics CRM Online had even fewer (well actually no real) alternatives since the application is hosted by Microsoft directly.

Ok, so we have established a gap in including web pages to our solution…is that it?! Well, what about our JScripts? I’m not sure about you but as much as I enjoy cutting lines of JScript on a Friday night, the prospect of copying and pasting the same script over and over again on various onChange, onLoad, and onSave events doesn’t thrill me. Could I do this in earlier versions? As I always tell my students in the Microsoft Dynamics CRM customization course I teach, “It is not a matter of can I do it…it is a matter of should I do it.” I have always had the option of hacking away at some .JS files on my web server or something like but it is a risky venture and potentially not upgradeable.
So what is a lowly customizer to do? Microsoft Dynamics CRM 2011 to the rescue.

Web Resources
There are a number of available Web Resources at our disposal.

  • HTML Web Pages
  • Style Sheets (CSS)
  • Script (JScript)
  • Data (XML)
  • Images(PNG, JPG, GIF, or ICO)
  • Silverlight (XAP)
  • Style Sheets (XSL)

The one that provides the most frequent re-use in my personal opinion would be the use of the Script Web Resource. I am working on three additional posts concerning Web Resources:

  • HTML Web Page
  • Silverlight
  • Data XML

Each of these posts will also incorporate the use of either CSS, XSL, and Image Web Resources as well. For this post, however, I will be focusing on the use of Scripting.

Let’s take a look at a specific example:

JScript
In this example, we are going to create a simple JScript which hides the notes tab on nearly any CRM form. In addition, this example is operating within the context of a custom solution record named, “Summit Group Software” (yeah I know if is a blatant plug for Summit Group but…..you are on our blog so I guess it comes with the territory.) Additionally, this example assumes you have added a Bit attribute named, “new_show” to the Account entity and have it on the form.

Task 1 – Create a new JavaScript Library

In this task, you are going to setup new JavaScript library that will be used later in this exercise.

While still in the Summit Group Software solution, click New from the toolbar and select Web Resource from the list.

Enter the following Information in the New Web Resource Dialog:

  • Name= “HideNotes”
  • Display Name= “Hide Notes”
  • Type= “Script (Jscript)”

    Click the Text Editor button

    Paste in the following script in to the source dialog.

    function hidemynotes(eventContext)

    {

    if (Xrm.Page.data.entity.attributes.get(“new_show”).getValue() == 1)

    {

    // Show the Notes tab

    Xrm.Page.ui.tabs.get(“notes”).setVisible(true);

    }

    else

    {

    // Hide the Notes tab

    Xrm.Page.ui.tabs.get(“notes”).setVisible(false);

    }

    }

    Click OK

    Click Save and Close

    Task 2 – Attach Form onLoad event handler
    Next we need to setup the event handlers on the Account form
    While still in the Summit Group Software Solution Click on the Entities -> Add Existing

    Select Account in the entity list and then click OK.

    As soon as the Account entity appears in your list of available entities in your solution, expand the Account node and then expand the Forms node.

    In the forms grid, double-click Information form type Main
    Now that you are in the form editor we need to attach the event handler. To both the Form onLoad and the Show OnChange events.

    Click Form Properties in the ribbon to bring up the dialog.

    Click the Form Libraries Add button.
    In the lookup dialog select the “Hide Notes” web resource and Click OK

    In the Event Handler section Click Add.

    In the Handler Properties dialog do the following:

    • Function = ” hidemynotes”
    • Check Enabled
    • Check Pass execution context as first parameter

    Click OK, to close the Handler Properties dialog
    Click OK, to close the Form Properties dialog

    Task 3 – Attaching onChange event handler

    Next, we need to attach the event handler to the Show radio button.

    In the form editor click to select the “Show” control

    Click Change Properties button in the ribbon.

    In the Field Properties dialog, select the Events tab.

    Click the Add button in the Event Handlers grid.

    In the Handler Properties dialog do the following:

    • Function = ” hidemynotes”
    • Check Enabled
    • Check Pass execution context as first parameter

    Click OK, to close the Handler Properties dialog
    Click OK, to close the Field Properties dialog

    Click Save and Close


    Task 3 – Publishing changes and see the results
    While still in the Test, Click Publish All Customizations in the top toolbar of the Solution Explorer.

    Close the Solutions explorer
    In the main application window Navigate to the Account entity in the Workplace.
    In the ribbon, click the New button on the Account tab.

    The Account form loads and the Notes tab is hidden.

    Enter the following items:

    • Name = “Test Company”
    • Show= “Yes”
    • Once the Show equals “Yes” you will see the Notes Tab.

    This is a simple example of a Web Resource. In the near future, you should expect additional articles on the use of the other kinds of Web Resources. Likewise, I will be putting together an article or two on the changes to how we interact with the CRM form through our Jscript. You may have noticed in the code example, there are a lot of syntaxual differences between earlier version of Microsoft Dynamics CRM and 2011. The other thing you may have picked up on if you are a customizer/developer on CRM 4.0 is that I hide a tab. Yes, I know that is technically not supported in 4.0…but…..in 2011, IT IS! We now have the ability to hide fields, labels, sections, tabs, any button…you name it. And, again, all in a supported fashion.

    No comments:

    Post a Comment