Create Context Sensitive Menu
Tuesday, February 26, 2008, 16:37 - SharePoint
Posted by Administrator
Create sensitive menu using Core.js
In the Core.js file the main function responsible for the document library drop down menus is AddDocLibMenuItems(m, ctx) and this provides a hook by which we can insert additional menu items.
The first few lines of the function are as follows
if (typeof(Custom_AddDocLibMenuItems) !="undefined")
if (Custom_AddDocLibMenuItems(m, ctx))
It is basically looking as to whether a function named Custom_AddDocLibMenuItems is defined or not, if it is defined then it simply calls it. So the main idea here is to override this Custom_AddDocLibMenuItems function by our own script which we can use to extend the context menu, adding new items.
Now we can override the Custom_AddDocLibMenuItems function in core.js adding our own script as follows.
function Custom_AddDocLibMenuItems(m, ctx)How does the function above works?
var strDisplayTextCustom = 'View Excel Sheet in Browser';
if(currentItemAppName.toLowerCase() == "microsoft office excel")
var strImagePath = ctx.imagesPath+"XLS16.GIF";
// Add menu item
CAMOpt(m, strDisplayTextCustom, strAction, strImagePath);
// add a separator to the menu
The Custom_AddDocLibMenuItems function takes two parameters m and ctx, the first parameter m is basically representing the menu object itself, where as ctx provides HTTP context information about the web request. To add an item to the menu only one function call is needed.
CAMOpt(m, strDisplayTextCustom, strAction, strImagePath);The CAMOpt function takes 4 parameters which are as follows:
1) The menu object to add item to.
2) The display text of the menu item.
4) Path to an image associated with the menu item.
CAMSep function simply adds a separator bar to the menu. Finally the function returns a false which means that standard menu items should also be included, if we had returned true then standard menu items would not have been included.
But when a new service pack is released, the new added Custom_AddDocLibMenuItems method could be deleted!
Create sensitive menu using webpart
When you open the document library where you want to create a context sensitive menu, you can add a webpart. Select Edit Page
add a new content editor webpart and add the function from above in this source code.
When you open this document library now, you'll see that when you open the menu the custom menu items appears. And when a new service pack replaces the core.js your context sensitive menu's are save.