Disable Subgrids with Javascript in Dynamics CRM 2011

Paul Nieuwelaar, 28 February 2012

While customizing a form in Dynamics CRM 2011, you can set whether a field should be Read-Only or not, this is done through each of the field properties. This does not require any Javascript, and is useful when storing data that is automatically generated via a plugin or javascript, and which you don’t want users to modify.

Unfortunately, the same functionality is not supported with Subgrids. When editing subgrids’ properties, there is no option for setting ‘Read-Only’, however using a small piece of Javascript, we are able to disable any Subgrid on a form.

 Disable Subgrids with Javascript in Dynamics CRM 2011

I first noticed the ability to have disabled subgrids when I was testing a security role that had read privileges for a certain entity, but not update. When viewing a subgrid on the form for that entity, it appeared to be disabled (read-only). I investigated the HTML behind the subgrid using IE dev tools, and found out when it was being set. I then put this together in a javascript function that could be used at any time, where ever you want.

I’ve added a code snippet of the function below. Just add this into a javascript web resource, and then call this function from the forms onload event, passing in the subgrid name as a parameter:

 Disable Subgrids with Javascript in Dynamics CRM 2011

Please note that this method is unsupported.

// Disable a subgrid on a form
function disableSubgrid(subgridName) {
    document.getElementById(subgridName + "_span").disabled = "true";
}

This code can be useful for disabling subgrids that are only there for read-only purposes, such as displaying relationships generated by plugins that users should never modify. This code can also be extended to disabling subgrids for certain users, since you might want a user to have update access to an entity, but not to be able to add or remove relationships.

When viewing the disabled subgrid, it will appear to be ‘greyed out’. You will still be able to read all the rows in the grid, and even double click to open the records. When you click into the subgrid however, the ribbon will not change, and so records will not be able to be created or deleted.

 Disable Subgrids with Javascript in Dynamics CRM 2011