Form Script Bundling and Minification

Ever since form script loading was made async, I have tried different ways to ensure that form scripts will be loaded in the sequence I want, and not how CRM wants it to be, a.k.a random. CRM2015 Update 1 has introduced Turbo Forms. Turbo Forms are supposed to drastically improve form render time. The problem of managing script dependencies, is still left to the developer. You basically have two options:

  1. In build stage, bundle and minify the scripts in the order of dependencies and use this in the form.
  2. Write self contained scripts, without any dependencies.

I tried out an approach using Plugin and couple of entities to try solve this. If you would rather read the code, you can download the source from I have also included the managed and unmanaged solutions in the repo.


1. Form Load sequence entity, that stores the entity name and scripts that are to be loaded
 2. A blank javascript webresource, whose name is in this pattern: [entityname].crmform.min.js
 3. A plugin the runs on post-RetrieveMultiple on webresource entity

How it is wired up

[entityname].crmform.min.js is added to the form, that requires bunding and minification. This script just contains a comment, and nothing else.

Create a Form Load Sequence record, that specifies the entity name and the scripts that loaded be minified for this entity.

Register the plugin on post RetrieveMultiple of webresource entity.

How it works

If you have read the plugin code already, you already know. But you haven’t here is how it works:
The plugin retrieves the Query key on the PluginExecutionContext’s InputParameter that contains the QueryExpression object. It then checks, if there is a condition on this QueryExpression with name like crmform.min.js. If so, it retrieves the correct Form Load sequence record for the current entity.

Using the script sequence specified, it also retrieves the script webresources, concatenates and minifies them. This concatenated script is then used to update the OutputParameter’s BusinessEntityCollection. The plugin also checks if the javascript webresources specified in the form load sequence entity, actually exists. It displays an error, if it doesn’t.

ExecutionContext for RetrieveMultiple on webresource – Post Stage

  <Configuration i:nil="true" xmlns:i="" />
  <ConstructorException i:nil="true" xmlns:i="" />
    <z:anyType i:type="PluginExecutionContext" xmlns:z="" xmlns:i="">
      <InputParameters xmlns:a="" xmlns:b="">
          <b:value i:type="a:QueryExpression">
              <a:Columns xmlns:c="">
                  <a:Values xmlns:c="">
                    <c:anyType i:type="d:string" xmlns:d="">ryr_formexperiment.crmform.min.js</c:anyType>
                  <a:EntityName i:nil="true" />
              <a:Filters />
            <a:LinkEntities />
            <a:Orders />
              <a:PagingCookie i:nil="true" />
      <OutputParameters xmlns:a="" xmlns:b="">
          <b:value i:type="a:EntityCollection">
                    <b:value i:type="z:guid">02fe0b47-8800-e511-80ef-c4346bada558</b:value>
                    <b:value i:type="c:string" xmlns:c="">ryr_formexperiment.crmform.min.js</b:value>
                    <b:value i:type="c:string" xmlns:c="">Ly9yeXJfZm9ybWV4cGVyaW1lbnQuY3JtZm9ybS5taW4uanM=</b:value>
                    <b:value i:type="a:OptionSetValue">
                <a:EntityState i:nil="true" />
                    <b:value>Script (JScript)</b:value>
                <a:KeyAttributes xmlns:c="" />
                <a:RelatedEntities />
            <a:PagingCookie><cookie page="1"><webresourceid last="{02FE0B47-8800-E511-80EF-C4346BADA558}" first="{02FE0B47-8800-E511-80EF-C4346BADA558}" /></cookie></a:PagingCookie>
      <OwningExtension xmlns:a="">
        <a:KeyAttributes xmlns:b="" xmlns:c="" />
        <a:Name>RYR.Experiments.PreRetrieveMultipleWebResource: RetrieveMultiple of webresource (Profiler)</a:Name>
        <a:RowVersion i:nil="true" />
      <PostEntityImages xmlns:a="" xmlns:b="" />
      <PreEntityImages xmlns:a="" xmlns:b="" />
      <RequestId i:nil="true" />
      <SharedVariables xmlns:a="" xmlns:b="" />
      <ParentContext i:nil="true" />
  <ExecutionException i:nil="true" xmlns:i="" />
  <ReplayEvents xmlns:a="" />
  <SecureConfiguration i:nil="true" xmlns:i="" />
  <WorkflowInputParameters xmlns:a="" />
  <WorkflowOutputParameters xmlns:a="" />
  <WorkflowStepId i:nil="true" xmlns:i="" />

There is a bit of overhead, as the plugin has to retrieve and minify the webresources.

Scripts minified at build stage and added to the form

It takes 291ms.

Script dynamically bundled and minified by plugin

It takes 481ms.

Take this numbers, with a pinch of salt, as I found the performance can vary quite a bit depending on the time of the day, as I tested this in CRMOnline. I also tested this with cache disabled.

Failures along the way
I tried these approaches which, didn’t quite workout, but I want to document them for future reference.

  1. Adding a non-existent form script into the Form Experiment‘s FormXml doesn’t work, even though there is plugin to take care of the RetrieveMultiple request. CRM doesn’t allow this to happen. The Javascript webresource has to exist, if you want to add this to a form. Here is what you’ll have to add to the root node of the formxml, to hook up the webresource and associated onsave and onload event handlers for the form
 <Library name='[JSWEBRESNAME]' libraryUniqueId='[NEWGUID]' />
 <event name='onload' application='false' active='false'>
   <Handler functionName='[FUNCNAME]' libraryName='[JSWEBRESNAME]' handlerUniqueId='[NEWGUID]' enabled='true' parameters='' passExecutionContext='false' />
 <event name='onsave' application='false' active='false'>
   <Handler functionName='[FUNCNAME]' libraryName='[JSWEBRESNAME]' handlerUniqueId='[NEWGUID]' enabled='true' parameters='' passExecutionContext='false' />


My previous posts on the same subject



  1. Scott Durow – Ghost Webresources
  2. Ben Hosk – Turbo Forms
  3. MSDN – Write Code for CRM Forms

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s