Monday, June 01, 2009

Hiding Menu Items from top menu bar in CRM 4.0

Here is a little script which you can call to hide menu item from top menu.
Put this script on page load or on any other event where you would like it.
You might also wanna add it as document.HideMenuItem = new function(targetMenu, targetMenuItem) {//code goes here};

function HideMenuItem(targetMenu, targetMenuItem) {
var menuLIs = document.getElementById("mnuBar1").getElementsByTagName("LI");
for (var i = 0; i < menuLIs.length; i++) {
if (menuLIs[i].title && menuLIs[i].title.indexOf(targetMenu) > -1) {
var targetDivs = menuLIs[i].getElementsByTagName("DIV");
for (var j = 0; j < targetDivs.length; j++) {
var targetLIs = targetDivs[j].getElementsByTagName("LI");
for (var k = 0; k < targetLIs.length; k++) {
if (targetLIs[k].innerHTML.indexOf(targetMenuItem) > -1) {
targetLIs[k].style.display = "none";
return;
}
}
}
}
}
}

All you have to do is call it with two parameters first parameter is the text without space so if you have root menu named Sub Test, you should call it SubTest. And the second parameter is the whole text of the menu item you need to hide.

HideMenuItem("ISV.NEW", "Coming Soon...");
HideMenuItem("SubTest", "Test Sub 1");
HideMenuItem("ISV.NEW", "Web Only");

Let me know if you need any more info on the same.
PS: Thanks to Andrew, there was a bug in the previous version of script, which didn't work with all the menus but now it has been fixed.

14 comments:

Anonymous said...

Excellent Post, many thanks Chinmay.
Steve

Chinmay said...

Thanks Steve

Chinmay

Anonymous said...

Hi Chinmay,

I want to hide the "Deactivate Account" at Account form.

I put HideMenuItem("Actions", "Deactivate Account"); at my onload event. But it is not working.

Please help. Thanks

Andrew

Chinmay said...

Hi Andres,

Thank you very much, there was a bug withing the script, I've fixed it and now the script is working all fine. Let me know if you find any other issue with this script.

Regards,
Chinmay

Unknown said...

Please update the script. It will be helpful us.

Chinmay said...

Hi Stalin,

Didn't get you? If you read my comment on the same, I've already updated it. Is there something else that needs to be fixed?

Unknown said...

I tried with HideMenuItem("File", "Close"); for testing. But it's not working. Anything am I doing wrong?

Lénárd said...

I'v correctid the code and for me it works. :)

HideMenuItem("Actions", "Deactivate Account");

function HideMenuItem(targetMenu, targetMenuItem) {

var menuLIs = document.getElementById("mnuBar1").getElementsByTagName("LI");

for (var i = 0; i < menuLIs.length; i++) {

if (menuLIs[i].title && menuLIs[i].title.indexOf(targetMenu) > -1) {

var targetDivs = menuLIs[i].getElementsByTagName("DIV");
for (var j = 0; j < targetDivs.length; j++) {
var targetLIs = targetDivs[j].getElementsByTagName("LI");
for (var k = 0; k < targetLIs.length; k++) {
if (targetLIs[k].innerHTML.indexOf(targetMenuItem) > -1) {
targetLIs[k].style.display = "none";
return;
}
}
}
}
}
}

Lénárd said...

Hi!
I'v corrected the code, and for me it works! :)

HideMenuItem("Actions", "Deactivate Account");

function HideMenuItem(targetMenu, targetMenuItem) {

var menuLIs = document.getElementById("mnuBar1").getElementsByTagName("LI");

for (var i = 0; i < menuLIs.length; i++) {

if (menuLIs[i].title && menuLIs[i].title.indexOf(targetMenu) > -1) {

var targetDivs = menuLIs[i].getElementsByTagName("DIV");
for (var j = 0; j < targetDivs.length; j++) {
var targetLIs = targetDivs[j].getElementsByTagName("LI");
for (var k = 0; k < targetLIs.length; k++) {
if (targetLIs[k].innerHTML.indexOf(targetMenuItem) > -1) {
targetLIs[k].style.display = "none";
return;
}
}
}
}
}
}

Chinmay said...

Thanks Lenard. I've fixed the code as well :) some mistake on my side or Google's editor converted < and > to HTML codes. If there was any other mistake please do let me know.

Regards,
Chinmay

Mike shaw said...

Is there any way this could be used to rename a menu item - I would like to rename 'Convert to Case' to 'Convert to Enquiry'?

Dynamics 365 Specialist said...

For any custom menu there should be a slight change:
if (toolBar != null && toolBar.rows.length > 0 && toolBar.rows[0].cells.length > 0) will always be invalid. Remove this check and things will work fine.

Dynamics 365 Specialist said...

For any custom menus remove following check and this script works like a charm:

if (toolBar != null && toolBar.rows.length > 0 && toolBar.rows[0].cells.length > 0)

Dam Ske said...

is it working for for custom activity ???