Saturday, August 15, 2009

Changing sequence and adding new menu items in SharePoint

Hi All,

As I have explained you in my previous post about the menu items in the toolbar on AllItems.aspx page. We will take the same code and sample here and I will explain you today how to play with the Menu items inside the menus.

So this post is all about changing the sequence of menu items and adding new menu items.

First let me show you how you can change the position of individual items in the menu. Considering we are playing with Actions menu and we want to change the “View RSS Feed” to appear with the Edit in Datasheet option.

Ok, let me clarify one thing very clearly. We should not change the default place of menu items as they are actually categorized properly and they have been placed in respective group items. For example, it doesn’t make sense if you place Alert Me option with Edit in Datasheet option. Does it make sense to have this option with Datasheet option? Answer is absolutely no.

But this is just for fun purpose and also to give you an idea about how to do that. Before we actually go in to the code, I would like to share here that each of these menu options fall under MenuGroupID.

First observe the below figure closely. See Red circle, Bur circle and green circle.



Now see the below image of menu items



See, There is only one red circle because see the first category. We have only one single item here which is EditInDatasheet. See two blue circles, we have two items in the menu category (Export to Spreadsheet and Open with Access) and last we have again two circles shown in green color represents last two options. (View RSS Feed and Alert Me). As you can see all belongs to MenuGroupId. First item has id number 200; second two items have id number 400 and last two items have id number 500.

So now we will play the trick and we will move View RSS Feed with EditinDatasheet. All we need to do is just change the MenuGroupId of RSS Feed from 500 to 200 so that it will come with ExportinDatasheet.

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").MenuGroupId = 200;
}

break;
}
CheckControl(childControl);
}


See the effect of this code,



If we want View RSS Feed to appear first and Datashet option to come second, then just a little modification will do this for us. We only need to add one single line only.

if (Menu.GetMenuItem("ViewRSS") != null)
{
Menu.GetMenuItem("ViewRSS").MenuGroupId = 200;
Menu.GetMenuItem("ViewRSS").Sequence = 0;
}




This is the way you can change the menu item from one place to the other place.

Now how about adding a menu item template to Menu? Let us go ahead and add menu item template to the Actions menu.

MenuItemTemplate objTemplate = Menu.AddMenuItem("MailMeID", "Mail Me From Hotmail",
"/_layouts/images/SPSPROFL.GIF",
"This will take you to hotmail", "", strClientURL);

objTemplate.MenuGroupId = 200;





Once you click on this item, it will take you to hotmail.com. Ok, now how about adding a client side code to it? As you can see in my previous code, I kept the last parameter blank. That is where we need to right the client event.

Just to demonstrate you, I have used the built in Client Click of RSS feed and assigned it to our new menu item.


if (childControl.ToString().ToUpper() == "Microsoft.SharePoint.WebControls.ActionsMenu".ToUpper())
{
ActionsMenu Menu = (ActionsMenu)childControl;
Menu.Visible = true;
string strClientURL = "window.location = '/sites/Test/_layouts/listfeed.aspx?List=%7BA6B5F63B%2DB860%2D418F%2DBEB0%2D8DAFC72C1C6B%7D';";
MenuItemTemplate objTemplate = Menu.AddMenuItem("MailMeID", "Mail Me From Hotmail",
"/_layouts/images/SPSPROFL.GIF",
"This will take you to hotmail", "", strClientURL);


objTemplate.MenuGroupId = 200;

break;
}


You can change this url to your desired page in the site. Once you click on Mail me from Hotmail, it will take you to RSS Feed page.



If you add one more single line of code which is shown below, we get that new menu item with separator. It separates it with the other menu items with one line. Note that we have commented the MenuGroupID assignment, so that it becomes a separate group.

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

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

string strClientURL = "window.location = '/sites/Test/_layouts/listfeed.aspx?List=%7BA6B5F63B%2DB860%2D418F%2DBEB0%2D8DAFC72C1C6B%7D';";

MenuItemTemplate objTemplate = Menu.AddMenuItem("MailMeID", "Mail Me From Hotmail",
"/_layouts/images/SPSPROFL.GIF",
"This will take you to hotmail", "", strClientURL);

Menu.AddMenuItemSeparator();

//objTemplate.MenuGroupId = 200;

break;
}




Ok, now once again let us get the DesigntimeHTML and see what the new things that we have got,



Check out the new menu item added. I have highlighted it with blue underline and also see the new MenuGroupID that we got. It is 2147483647. So now we know that if any other menu item we want to add with the same group, we need to assign this number to the new MenuItemtemplate as we saw in the beginning of this post.

I am finding more interesting about this, I will be eager to share it with you all-time supportive readers.

3 comments:

Anonymous said...

Thanks for sharing good stuff.

~Venkat

Peasi said...

Do I need to create a custom class to add MenuItemTemplate to document library settings menu ? If yes, Which class to derive from?

SharePoint Kings said...

Peasi,

its just a code which you can put in webpart.




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