Hide tab in any entity form in Microsoft Dynamics CRM 4 using Javascript in onLoad event of the form.

In many cases, you might want to remove one of the tabs of an entity form in Microsoft Dynamics CRM 4. This can be specifically helpful in two cases:

1- If you want to hide and show tabs depending on a picklist value selection such as contact type: Hide Work details if contact is of type internal contact for example

2- If you want to hide the Administration and Notes tab from an entity form while these tabs are locked and cannot be removed from the entity form.

To hide a tab in an entity form, you can use the following Javascript sample script:

//---------------------------Hide a tab by passing tab name to the function
HideTab('Administration');
//replace "Administration" with your tab name
function HideTab (tabText)
{
 var tab = FindTab(tabText);
 if (tab)
 {
  tab.style.display = "none";
 }
}
function FindTab(tabText)
{
 var tabBar = document.getElementById("crmTabBar");
 if (tabBar)
 {
  var tabs = tabBar.childNodes;  

  for (var i = 0, len = tabs.length; i < len; i++)
  {
   var currentTab = tabs[i];
   if (currentTab.innerText === tabText)
   {
    return currentTab;
   }
  }
 }
}
//-----------------end of script--------

Maxjerin has added a new code suggestion. Maxjerin’s code hides a tab based on the onChange event of a field on the form. If the primary contact “name” field is maxjerin, the tab is displayed. Otherwise the tab is hidden.

I have updated the post to include Maxjerin’s code – Thanks Maxjerin:

Here is the code that I used to HIDE/UNHIDE a tab when I wanted to show it for only one particular user and hide for others. The code includes some alert messages just for debugging purpose which you might find useful to understand the code. You may remove them once done.

This section goes into the onChange section of the Primary Contact lookup field

if (crmForm.all.primarycontactid.DataValue == null )
{
var strTabStatus = TabStatus(‘Profile’);
if ( strTabStatus == ”)
{
alert(“Primary Contact none, hiding the tab”);
HideTab(‘Profile’);
}
}
else if (crmForm.all.primarycontactid.DataValue[0].name == ‘maxjerin’)
{
alert(“Primary Contact p f, unhiding the tab”);
UnHideTab(‘Profile’);
//replace “Administration” with your tab name
}
else if (crmForm.all.primarycontactid.DataValue[0].name != ‘maxjerin’)
{
var strTabStatus = TabStatus(‘Profile’);
if (strTabStatus == ” )
{
alert(“Primary Contact other than maxjerin, Hiding the tab”);
HideTab(‘Profile’);
}
}
function HideTab (tabText)
{
var tab = FindTab(tabText);
if (tab)
{
tab.style.display = “none”;
}
}
function UnHideTab (tabText)
{
var tab = FindTab(tabText);
if (tab)
{
tab.style.display = “”;
}
}
function FindTab(tabText)
{
var tabBar = document.getElementById(“crmTabBar”);
if (tabBar)
{
var tabs = tabBar.childNodes;
for (var i = 0, len = tabs.length; i < len; i++)
{
var currentTab = tabs[i];
if (currentTab.innerText === tabText)
{
return currentTab;
}
}
}
}
function TabStatus(tabText)
{
var tabBar = document.getElementById(“crmTabBar”);
if (tabBar)
{
var tabs = tabBar.childNodes;
for (var i = 0, len = tabs.length; i < len; i++)
{
var currentTab = tabs[i];
if (currentTab.innerText === tabText)
{
if (currentTab.style.display == ‘none’)
{
alert(“tab already hidden, TAB NAME: ” + currentTab.innerText );
return “none”;
}
else
{
alert(“tab is visible, TAB NAME: ” + currentTab.innerText );
return “”;
}
}
}
}
}
//-----------------------------------------------------------------------
Please comment below if you have any feedback or anything to add.
Thanks

6 Replies to “Hide tab in any entity form in Microsoft Dynamics CRM 4 using Javascript in onLoad event of the form.”

  1. Hey Ibrahim,

    Could you also give a description on how to reverse the process, I wanted to hide/unhide based on the onChange event of a lookup field

  2. Here is the code that I used to HIDE/UNHIDE a tab when I wanted to show it for only one particular user and hide for others. The code includes some alert messages just for debugging purpose which you might find useful to understand the code. You may remove them once done.

    This section goes into the onChange section of the Primary Contact lookup field

    if (crmForm.all.primarycontactid.DataValue == null )
    {
    var strTabStatus = TabStatus(‘Profile’);
    if ( strTabStatus == ”)
    {
    alert(“Primary Contact none, hiding the tab”);
    HideTab(‘Profile’);
    }
    }
    else if (crmForm.all.primarycontactid.DataValue[0].name == ‘maxjerin’)
    {
    alert(“Primary Contact p f, unhiding the tab”);
    UnHideTab(‘Profile’);
    //replace “Administration” with your tab name
    }
    else if (crmForm.all.primarycontactid.DataValue[0].name != ‘maxjerin’)
    {
    var strTabStatus = TabStatus(‘Profile’);
    if (strTabStatus == ” )
    {
    alert(“Primary Contact other than maxjerin, Hiding the tab”);
    HideTab(‘Profile’);
    }
    }

    function HideTab (tabText)
    {
    var tab = FindTab(tabText);
    if (tab)
    {
    tab.style.display = “none”;
    }
    }

    function UnHideTab (tabText)
    {
    var tab = FindTab(tabText);
    if (tab)
    {
    tab.style.display = “”;
    }
    }

    function FindTab(tabText)
    {
    var tabBar = document.getElementById(“crmTabBar”);
    if (tabBar)
    {
    var tabs = tabBar.childNodes;

    for (var i = 0, len = tabs.length; i < len; i++)
    {
    var currentTab = tabs[i];
    if (currentTab.innerText === tabText)
    {
    return currentTab;
    }
    }
    }
    }

    function TabStatus(tabText)
    {
    var tabBar = document.getElementById(“crmTabBar”);
    if (tabBar)
    {
    var tabs = tabBar.childNodes;

    for (var i = 0, len = tabs.length; i < len; i++)
    {
    var currentTab = tabs[i];
    if (currentTab.innerText === tabText)
    {
    if (currentTab.style.display == ‘none’)
    {
    alert(“tab already hidden, TAB NAME: ” + currentTab.innerText );
    return “none”;
    }
    else
    {
    alert(“tab is visible, TAB NAME: ” + currentTab.innerText );
    return “”;
    }
    }
    }
    }
    }

    Cheers,
    maxjerin

  3. Hello Maxjerin,

    I’m administering a small on-premise CRM 4.0 deployment and have tried to use your scripts. I am able to get the HideTab(‘administration’) script working because of how easy it is however the latter conditional hide (primarycontactid) script is not quite what I need it to be.

    I am trying to hide a tab within Opportunities based on an assigned Security Role. I was going to repurpose the latter script using the RoleID from within _MSCRM.RoleBase however my javascripting skills just aren’t up to par.

    Do you have any suggestions for an OnLoad script that would accomplish that Security Role based tab hide within Opportunities? Any help would be great!

    Thanks!!

  4. Hi,
    Thank you for the script of hide/unhide for some user.Please help for debug the script.
    1. My lookup field name : ag_productid
    2. My Tab name : Product
    3. Name : crmadmin

    Thank you in advance

    if (crmForm.all.ag_productid.DataValue == null )
    {
    var strTabStatus = TabStatus(‘Product’);// Tab name
    if ( strTabStatus == ”)
    {
    alert(“ag_productid none, hiding the tab”); //
    HideTab(‘Product’);
    }
    }
    else if (crmForm.all.ag_productid.DataValue[0].name == ‘crmadmin’)
    {
    alert(“ag_productid p f, unhiding the tab”);
    UnHideTab(‘Product’);
    //replace “Administration” with your tab name
    }
    else if (crmForm.all.ag_productid.DataValue[0].name != ‘crmadmin’)
    {
    var strTabStatus = TabStatus(‘Product’);
    if (strTabStatus == ” )
    {
    alert(“ag_productid other than crmadmin, Hiding the tab”);
    HideTab(‘Product’);
    }
    }

Please comment or leave feedback