Create sensitive menu using Core.js
The context menu is itself a JavaScript program means that the context menu is generated by a JavaScript code. This JavaScript program can be found at
\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS\1033\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))
return;
}
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)
{
var strDisplayTextCustom = 'View Excel Sheet in Browser';
setDocType();
if(currentItemAppName.toLowerCase() == “microsoft office excel”)
{
strAction=”alert(ctx.HttpRoot+unescapeProperly(currentItemFileUrl))”;
var strImagePath = ctx.imagesPath+”XLS16.GIF”;
// Add menu item
CAMOpt(m, strDisplayTextCustom, strAction, strImagePath);
// add a separator to the menu
CAMSep(m);
}
return false;
}
How does the function above works?
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.
3) Javascript action to perform when item is clicked.
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.
VN:F [1.9.20_1166]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.20_1166]