Saturday, August 15, 2009

Hiding / Removing Menu and Menu items in SharePoint

Hi All,

Today we are going to discuss something on tool bar action. We all know that we get toolbar where we have New menu, action menu available when we are in AllItems.aspx.

Let us see how we can remove the toolbar specific item, means sometime we may require removing NewMenu and sometimes we may want to remove ActionMenu or even from Settings menu, then here is a way we can do this.

We can also hide specific item from New Menu, Action Menu or Settings menu. First we need to iterate through all controls on AllItems.aspx and once we find the specific menu we are looking for, then we can cast that control in to specific menu and then call visible method to set it to true and false.

First let me give you a highlight on what are these classes.

Microsoft.SharePoint.WebControls.NewMenu
Microsoft.SharePoint.WebControls.ActionsMenu
Microsoft.SharePoint.WebControls.SettingsMenu


So to demonstrate, go ahead and create one class which inherit from web part class and write down the following code in createchildcontrol event. Deploy it on your site and edit the AllItems page that you want to work with and add the webpart above AllItems web part (form web part).

protected override void CreateChildControls()
{
foreach (Control ctrl in this.Page.Controls)
{
CheckControl(ctrl);
}
base.CreateChildControls();
}


Now, in this code we will check each control on the page and once we find our required control, then we will cast that control and set the visible property.

private void CheckControl(Control ParentControl)
{
try
{
foreach (Control childControl in ParentControl.Controls)
{

if (childControl.ToString().ToUpper() == "Microsoft.SharePoint.WebControls.NewMenu".ToUpper())
{
NewMenu Menu = (NewMenu)childControl;
Menu.Visible = false;

if (Menu.GetMenuItem("NewFolder") != null)
Menu.GetMenuItem("NewFolder").Visible = false;
break;
}
CheckControl(childControl);
}
}
}


Above code will iterate all controls till it finds the New Menu and once it gets the New Menu, we cast the control in the NewMenu class object and set its property to false, means we cannot see NewMenu.

Below figure shows the result of above code.



Same way, if we run the below code

foreach (Control childControl in ParentControl.Controls)
{

if (childControl.ToString().ToUpper() == "Microsoft.SharePoint.WebControls.ActionsMenu".ToUpper())
{

ActionsMenu Menu = (ActionsMenu)childControl;
Menu.Visible = false;
break;
}
CheckControl(childControl);
}


Then, you can see the result as shown in the below screen



Similarly, if you write the below code,

foreach (Control childControl in ParentControl.Controls)
{

if (childControl.ToString().ToUpper() == "Microsoft.SharePoint.WebControls.SettingsMenu".ToUpper())
{
SettingsMenu Menu = (SettingsMenu)childControl;
Menu.Visible = false;
break;
}
CheckControl(childControl);
}


Then, you can see the result as shown in the below screen.



Ok, let us proceed. From the List Settings option, I have enabled to new folder option.



So ideally you will find New Folder option when you click in New from the tool bar. However if you write the following code, then it will hide the New Folder option.

foreach (Control childControl in ParentControl.Controls)
{

if (childControl.ToString().ToUpper() == "Microsoft.SharePoint.WebControls.NewMenu".ToUpper())
{
NewMenu Menu = (NewMenu)childControl;
Menu.Visible = true;

if (Menu.GetMenuItem("NewFolder") != null)
Menu.GetMenuItem("NewFolder").Visible = false;
break;
}
CheckControl(childControl);
}




Ok, same goes for the other menu items in the other menu as well. For example, let’s remove the Alert Me option from Actions menu. Question is how to do that? In above example, we used NewFolder as MenuItem and found it. But what needs to be passed as parameter in GetMenuItem method?

Let me give you a wonderful trick. Even I was not aware about this. I worked on it and find out that it’s the ID of the MenuItem. Where to find that ID, I’ll tell you how I found it. See the below figure. Once, you come in the IF condition of your menu option, set a break point and then observe the following red circle marks. This is the Menu.GetDesignTimeHTML() method that you can check in local window or immediate window. I have taken this HTML after my debug point crossed the code that makes “Alert Me” visible false, that is the reason you cannot find Alert Me text here. If you observe this HTML before the code gets execute from that point, you will be able to see that as well.



Use the ID of the menu item in code. Just remember avoid everything including _ character in the ID and take the rest of all character and pass it to the GetMenuItem method.

And now see the below code

foreach (Control childControl in ParentControl.Controls)
{

if (childControl.ToString().ToUpper() == "Microsoft.SharePoint.WebControls.ActionsMenu".ToUpper())
{

ActionsMenu Menu = (ActionsMenu)childControl;
Menu.Visible = true;

if (Menu.GetMenuItem("SubscribeButton") != null)
Menu.GetMenuItem("SubscribeButton").Visible = false;

break;
}
CheckControl(childControl);
}


So, from where I come to know that I have to pass “SubscribeButton” as parameter? Yes, its from the designtimHTML. Once you get all the code, then you can use it.

So after executing the above code, see the result below.” Alert me” menu item is not there in Actions menu.



So if I want to hide View RSs Feed, I will use the ID “ViewRSS” and run the code and I will get the result. (Check the red circle in the DesigntimeHTML figure).

foreach (Control childControl in ParentControl.Controls)
{

if (childControl.ToString().ToUpper() == "Microsoft.SharePoint.WebControls.ActionsMenu".ToUpper())
{

ActionsMenu Menu = (ActionsMenu)childControl;
Menu.Visible = true;

if (Menu.GetMenuItem("ViewRSS") != null)
Menu.GetMenuItem("ViewRSS").Visible = false;

break;
}
CheckControl(childControl);
}





Same way you can check the DesignTimeHTML for Settings menu and play with the MenuItem under Settings Menu.

Hope you’ve liked my research. Thank you in advance. :)

10 comments:

@m!th said...

I agree wiht this method, but is this applicable to SiteManager.aspx ?

Jackie said...

Is there a way to remove individual menu items for all items in a single document library? I don't want to have this functionality for all document libraries in a site collection or a site. Just an individual document library. Is this possible??? Thank you very much in advance. Have a great day!

Sincerely,
Jacqueline

SharePoint Kings said...

To @m!th, we had not tried this method on sitemanager.aspx but as per logic it will pick controls of page so it should work but the only problem is to where to put this code in application pages like sitemanager.aspx. give it a try it should work.

@Jackie, it is for individual library only. you create a web part and put that web part on any specific library only. it will hide menu of specific library only.

Jackie said...

Thank you very much. I will try it out tomorrow.

;-)

Viraj Vashi said...

Appreciated.It is really very good article and i tried also. It is working fine on New menu and settings menu
but i could not able to achieve for Action menu.
My code is following:
private void CheckControl(Control ParentControl)
{
foreach (Control childControl in ParentControl.Controls)
{
if (childControl.ToString().ToUpper() == "Microsoft.SharePoint.WebControls.SettingsMenu".ToUpper())
{
SettingsMenu Menu = (SettingsMenu)childControl;
Menu.Visible = false;
break;
}
CheckControl(childControl);
}
foreach (Control childControl in ParentControl.Controls)
{

if (childControl.ToString().ToUpper() == "Microsoft.SharePoint.WebControls.NewMenu".ToUpper())
{
NewMenu Menu = (NewMenu)childControl;
Menu.Visible = false;
break;
}
CheckControl(childControl);
}
foreach (Control childControl in ParentControl.Controls)
{
if (childControl.ToString().ToUpper() == "Microsoft.SharePoint.WebControls.ActionsMenu".ToUpper())
{
ActionsMenu Menu = (ActionsMenu)childControl;
Menu.Visible = false;
break;
}
CheckControl(childControl);

arActionMenu.Add(childControl.ToString().ToUpper());
}
}

I tried for only Actions menu also. ActionsMenu's object is not comming means it doesn't go inside
this if (childControl.ToString().ToUpper() == "Microsoft.SharePoint.WebControls.ActionsMenu".ToUpper())
condition.

Please help me for this. I would be very thankful to you.

Regards,
Viraj Vashi

SharePoint Kings said...

actually code should work. but you can refer this link if you want to do in javascript check this link

I hope this will help you out. :)

Unknown said...

Hi everybody.

I'm newbe on sharepoint development.

I can understand everything you say, but How can i implement that?

I mean: It is necessary to open a .net project? It is enough with open the aspx page with designer 2007. It necessary to add a new webpart for writting the code you told?

Your code solves part of my problem. The other part is hide or show the menu dependig on user rights. (really group). I can pull of the threat for that but i need a threat for pulling.

Somebody may help me????

SharePoint Kings said...

carlos,
if you want to remove from code then you must use .net project , write a code.

because in sharepoint designer you can do just a layout,look and feel changes but cannot apply code.

regarding the group permission, same thing can be achive from this code but for that you need to extend according to your business logic.
like check logged in user, find its group, check group permission, and remove unwanted menu(s)...

Unknown said...

Hi,
Thank you very much for this post. I was able to implement this successfully. Just one question. I managed to hide the 'All Documents' (far right side) link as well. But, one question. Is there anyway you can hide the text 'view' which appear left to 'All Documents'?

Many thanks,

Anu

SharePoint Kings said...

Anu,
we are not sure as we had not done that but at least
you can check view source, find particular tag, and make it display none.




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com