CRM 2013 Make All Controls (Including Subgrids) Disabled onLoad

So I came across a client requirement that basically meant I was going to have to make all controls disabled in the onLoad event of the form with script.  No problem right?  Just get all the controls set disabled to true, and BAM!  Only problem is there are subgrids on the forms, and they need to be disabled as well, and there is no supported way to really do this.  The other major issue is that subgrids don’t actually show up in the controls collection in the onLoad event (as of 3/3/2014).

Well, how to get around this?  The old trick of setting the subgrid span to disabled doesn’t work anymore (damn), so I looked into the disabled subgrid html on a Read Only form. Best I can tell, the disabling is being handled server-side and the main difference is the add and list expand buttons just aren’t rendered for a disabled grid.

OK, well with a little unsupported jQuery magic, I can remove the buttons.  The issue I had then was that the subgrids not being in the collection onLoad.  To get around that, I used setInterval to continuosly loop until the subgrids loaded and I could get them using Xrm.Page.  It doesn’t really make the form read only, of course, but it “fakes” it pretty well.  The code is below.

 


var intervalId;

function makeReadOnly()

{

try

{

var subgridsLoaded = false;

Xrm.Page.ui.controls.get().forEach(function (control, index)

{

if (control.setDisabled && Xrm.Page.ui.getFormType() != 3)

{

control.setDisabled(true);

}

else

{

removeAddButtonFromSubGrid(control);

subgridsLoaded = true;

}

});

if ($("div[id$='_crmGridTD']").length > 0 && !subgridsLoaded)

{

intervalId = setInterval(function ()

{

var subgridsArr = Xrm.Page.getControl(function (control, index)

{

return control.getControlType() == 'subgrid';

});

subgridsArr.forEach(function (control, index)

{

removeButtonsFromSubGrid(control);

});

}, 500);

}

}

catch (e)

{

alert("makeReadOnly() Error: " + e.message);

}

}

function removeButtonsFromSubGrid(subgridControl)

{

if (intervalId)

{

$('#' + subgridControl.getName() + '_addImageButton').css('display', 'none');

$('#' + subgridControl.getName() + '_openAssociatedGridViewImageButton').css('display', 'none');

clearInterval(intervalId);

}

}
Advertisements
Comments
5 Responses to “CRM 2013 Make All Controls (Including Subgrids) Disabled onLoad”
  1. Hugo says:

    Finally I found it! Just one thing missing… Did you find a way to hide the Delete button from the subgrid?
    thx!

    • yanbu0 says:

      You should be able to add another line of code above the clearInterval() call similar to the two lines above it to do this, I’ll try to find some time to add the code here shortly.

  2. stefanooch says:

    great stuff … I’m getting a few jscript errors.

    1. Is function removeAddButtonFromSubGrid missing or what is to be removeButtonsFromSubGrid
    2. Keep getting undefined $ errors.

    any suggestions would be great.

    • yanbu0 says:

      Having trouble understanding what you are asking with 1 above. The ‘$’ undefined errors seems to indicate jQuery isn’t loaded, so there may be something going on with your environment. This was an unsupported trick, so things may have changed in 2013, and almost certainly have in 2015. I got out of CRM consulting 6 months back so I haven’t been keeping up to see if this is still working or not.

    • Edaw says:

      Thanks for this post, really useful to know.
      Can’t believe CRM won’t allow a readonly property on a grid (!)

      @stefanooch :
      It’s a typo. Just change the removeAddButtonFromSubGrid to removeButtonsFromSubGrid.

      @yanbuo:
      Would the hiding of the delete button just be :
      $(‘#’ + subgridControl.getName() + ‘_deleteImageButton’).css(‘display’, ‘none’);

      Also, do you know of any way to stop the user from selecting a row and loading that record?

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: