Script to Hide Add Existing Record button and Add New Record button using Javascript in Microsoft Dyanmics CRM

If you want to hide the two top left  buttons in the assicated view of any entity,  Add Existing Record and Add New Record Buttons, you can use the following javascript code i the onLoad Event of the Microsofty Dyanmics CRM 4 Form.  This post relies on and is an extension to the existing useful post by Dave Hawes.

 //This javascript should be added to the onLoad event for the form of the entity where you want to hide buttons for the associated views of other entities

//-------Hide Add Existing Record Button Only----Method 1----
HideAssociatedViewButton('new_account_new_project', 'Add existing Project to this record');
function HideAssociatedViewButton(loadAreaId, buttonTitle)
{
    var navElement = document.getElementById('nav_' + loadAreaId);
    if (navElement != null)
    {
        navElement.onclick = function LoadAreaOverride()
        {
            // Call the original method to launch the navigation link
            loadArea(loadAreaId);
            var associatedViewIFrame = document.getElementById(loadAreaId + 'Frame');
            if (associatedViewIFrame != null)
            {
                associatedViewIFrame.onreadystatechange = function HideTitledButton()
                {
                    if (associatedViewIFrame.readyState == 'complete')
                    {
                        var iFrame = frames[window.event.srcElement.id];
                        var liElements = iFrame.document.getElementsByTagName('li');
                        for (var i = 0; i < liElements.length; i++)
                        {
                            if (liElements[i].getAttribute('title') == buttonTitle)
                            {
                                liElements[i].style.display = 'none'; //this will hide and align properly
                                liElements[i].style.visibility = 'hidden'; // this is needed when re-sizing cause button to re-appear.
                                break;
                            }
                        }
                    }
                }
            }
        }
    }
}

//—End of Hide Add existing button Only

//——————Hide Add New Record Button and Add Existing Button ———

//Hide Both Buttons-----------
HideAssociatedViewButtons('new_account_new_project', ['Add existing Project to this record', 'Add a new Project to this record']); //Where "Project" is a custom entity.
//OR Hide Add Existing Button Only-----Method 2-------
HideAssociatedViewButtons('new_account_new_project', ['Add existing Project to this record']);  //Where "Project" is a custom entity.

function HideAssociatedViewButtons(loadAreaId, buttonTitles){
    var navElement = document.getElementById('nav_' + loadAreaId);
   if (navElement != null)    {
        navElement.onclick = function LoadAreaOverride()        {
            // Call the original CRM method to launch the navigation link and create area iFrame
            loadArea(loadAreaId);
            HideViewButtons(document.getElementById(loadAreaId + 'Frame'), buttonTitles);
        }
    }
}

function HideViewButtons(Iframe, buttonTitles) {
    if (Iframe != null ) {
        Iframe.onreadystatechange = function HideTitledButtons() {
            if (Iframe.readyState == 'complete') {
                var iFrame = frames[window.event.srcElement.id];
                var liElements = iFrame.document.getElementsByTagName('li');

                for (var j = 0; j < buttonTitles.length; j++) {
                    for (var i = 0; i < liElements.length; i++) {
                        if (liElements[i].getAttribute('title') == buttonTitles[j]) {
                             liElements[i].style.display = 'none'; //this will hide and align properly
                             liElements[i].style.visibility = 'hidden'; // this is needed when re-sizing cause button to re-appear.
                            break;
                        }
                    }
                }
            }
        }
    }
} 

Credit goes to Dave Hawes Blog post mentioned above.

 

2 Replies to “Script to Hide Add Existing Record button and Add New Record button using Javascript in Microsoft Dyanmics CRM”

Please comment or leave feedback