Microsoft Gold Azure and Dynamics CRM Partner | Salesforce Experts

Here is a simple trick that will allow you to store JSON formatted data in a Web Resource in Microsoft Dynamics CRM.  This provides a much more developer-friendly alternative to the XML data type that CRM offers.  The benefits of using JSON to store and transfer data versus using XML are well known.  It can be done using far less code, and it is much more consistent between different browsers.  Having to support multiple browsers is difficult when working with XML data in javascript because different browsers (or even different versions of the same browser) can behave very differently.  Storing data as a JSON web resource is a much simpler approach, but unfortunately, CRM does not offer a JSON data type for a Web Resource.  Even a plain text type would work for that purpose as well, but that too is unavailable to us.  Luckily, there is a way that we can make this work using a JScript type Web Resource.

Where would you actually use this?  In my case, I’ve used this to store configuration details for a managed solution.  The same thing can be accomplished using an XML web resource or a using a custom entity. A JSON Web Resource is significantly easier and less code than working with an entity when loading the data.  The other problem with using an entity record to control solution settings is that records cannot be included in a solution whereas the JSON Web Resource can  be included in a solution.

Example

Start by creating a new jscript type Web Resource, let’s call it new_config.js.  Then define your object like this:

Next, create a custom configuration page, we will call it new_configuration.htm, as an html Web Resource, and include the script at the top of the file (this will load the string value).  To load the entire set of config values, use JSON.parse().  This can also be done on a CRM form by including the config.js library on the form.  Now, any other scripts on the page can access your custom settings without the need to perform any web service requests at all!

As mentioned, you can use the same “config = JSON.parse(configJSON);” command inside any form jscript that will give you access to these values whenever you need them.  To do this, add the new_config.js library to the form.  No additional web service calls or XML parsing is needed.

Now you might ask “But how do we update these values?”.  It’s true, without the ability to update, we might as well just hardcode an actual javascript object instead of as a JSON string, but storing this as a JSON string is what makes it so easy to update.  To update the Web Resource, use a REST call. For example, add this function, and its helpers, to the page:

Note: This example makes the assumption that the guid of the webresource is known in advance, which should usually not be a problem

One last thing that you need to remember: when you update a Web Resource, you must Publish before you will see the change take effect.  That can be done manually, or that can be done in javascript using a PublishAllXml SOAP request.

Lastly, here are the two re-usable helper functions you will need that get called by the onSaveClick function:

To see this technique in action, create a new solution in CRM and set the new_configuration.htm Web Resource as the configuration page for the solution.  You will see the default values in the text boxes, which you can update and save (remember to publish after!).  And finally, create an additional JScript Web Resource and set it to run onLoad of any form. Include the onLoad resource and the config resource as form libraries:

 Capture

 

After reading this, maybe you have some creative ideas of how to apply this technique that I haven’t yet thought of yet?  Let me know in the comments below if you’ve found any other clever uses for this technique.