Welcome
Monday, January 16, 2006
+Add Folder-Level Context Menu Items
Add Folder-Level Context Menu Items
Here’s how to do it: -Some of you may ask “Why the heck?”… Well, I just come up with the solutions, not the problemsJ
Naturally, there may be several folders with like names in a library. This is intended to be used for standard folders. For example, for time sheets kept as per standard in a folder named “TimeReports”.
The script below can be replaced and tailored to meet the requirements. Ideally you will have a custom js file, custom doclib definitions and have defined one or more custom AddDoclibMenuItems functions also. For this test, we’ll use the default AddDoclibMenuItems function
Make sure you create a backup of the ows.js if you are modifying this file directly and not using a custom.js file.
1. We need a little script to return the file’s immediate parent folder name.
This goes anywhere in your script file.
function GetFolderName()
{
var fileName = GetFileName(currentItemFileUrl);
var folderName = currentItemFileUrl;
// remove /filename.ext from path
folderName = folderName.replace('/' + fileName,'')
// get folder name
return folderName.substring(folderName.lastIndexOf('/')+1);
}
function GetFileName(inPathFile)
{
return inPathFile.substring(inPathFile.lastIndexOf('/')+1);
}
2. Insert the function to add a custom menu items
function AddCustomFolderMenu()
{
var retval = false; // if no custom menu is added, default return value
var fName = GetFolderName();
switch (fName)
{
// here is where you can get creative and do our own logic. Get data from an xml file etc…
case "NewFolder":
// add only the ‘Edit’ and checkin/out menu items to any folder named “New Folder”
setDocType();
strDisplayText = StBuildParam(L_EditIn_Text, currentItemAppName);
strAction = "EditInOffice2000('" + currentItemFileUrl + "', '" + currentItemAppName + "', '" + ctx.HttpRoot + "')";
strImagePath = ctx.imagesPath + currentItemIcon;
CAMOpt(m, strDisplayText + " 2000", strAction, strImagePath);
AddCheckinCheckoutMenuItem(m, ctx, currentItemEscapedFileUrl);
retval = true; // No more menu items to be added.
break;
case "test":
// for this example, we will add an item in addition to the default menu items
strDisplayText = "Generate PDF Document";
strAction = "STSNavigate('" + ctx.HttpRoot + "/_layouts/makepdf/makepdfversion.aspx?DocumentName=" + GetFileName(currentItemFileUrl) + "&SourceFolder=" + GetFileName(currentItemEscapedFileUrl) + "&DocumentType=" + docExt + "&RootFolder=" + GetSource() +"')";
strImagePath = ctx.imagesPath + "pdf.gif";
CAMOpt(m, strDisplayText, strAction, strImagePath);
retval = false; // on return, it will continue to add the default items.
break;
case "so on and so on…":
}
return retval;
}
3. Locate the function below:
We need to place the test in an appropriate location within the script, for example
function AddDocLibMenuItems(m, ctx)
{
…
4. Find the statement:
if (currentItemFSObjType != 1)
{
…
(I guess we can have custom menu items for folders, but we mostly want to act on items inside folders…)
5. Insert this code (immediately after if statement above)
Here is where we will call our switch/case to determine whether a custom menu is required. If the menu was added, we will break out and render our custom menu items only.
var retval = AddCustomFolderMenu(currentItemEscapedFileUrl);
if (retval == true)
{
window.onfocus = RefreshOnNextFocus;
return;
// if menu was added, we render and quit.
}
6. Done
Nothing further required… Except for a few folders with the names you have selected. Maybe a browser refresh ;)
Here’s how to do it: -Some of you may ask “Why the heck?”… Well, I just come up with the solutions, not the problemsJ
Naturally, there may be several folders with like names in a library. This is intended to be used for standard folders. For example, for time sheets kept as per standard in a folder named “TimeReports”.
The script below can be replaced and tailored to meet the requirements. Ideally you will have a custom js file, custom doclib definitions and have defined one or more custom AddDoclibMenuItems functions also. For this test, we’ll use the default AddDoclibMenuItems function
Make sure you create a backup of the ows.js if you are modifying this file directly and not using a custom.js file.
1. We need a little script to return the file’s immediate parent folder name.
This goes anywhere in your script file.
function GetFolderName()
{
var fileName = GetFileName(currentItemFileUrl);
var folderName = currentItemFileUrl;
// remove /filename.ext from path
folderName = folderName.replace('/' + fileName,'')
// get folder name
return folderName.substring(folderName.lastIndexOf('/')+1);
}
function GetFileName(inPathFile)
{
return inPathFile.substring(inPathFile.lastIndexOf('/')+1);
}
2. Insert the function to add a custom menu items
function AddCustomFolderMenu()
{
var retval = false; // if no custom menu is added, default return value
var fName = GetFolderName();
switch (fName)
{
// here is where you can get creative and do our own logic. Get data from an xml file etc…
case "NewFolder":
// add only the ‘Edit’ and checkin/out menu items to any folder named “New Folder”
setDocType();
strDisplayText = StBuildParam(L_EditIn_Text, currentItemAppName);
strAction = "EditInOffice2000('" + currentItemFileUrl + "', '" + currentItemAppName + "', '" + ctx.HttpRoot + "')";
strImagePath = ctx.imagesPath + currentItemIcon;
CAMOpt(m, strDisplayText + " 2000", strAction, strImagePath);
AddCheckinCheckoutMenuItem(m, ctx, currentItemEscapedFileUrl);
retval = true; // No more menu items to be added.
break;
case "test":
// for this example, we will add an item in addition to the default menu items
strDisplayText = "Generate PDF Document";
strAction = "STSNavigate('" + ctx.HttpRoot + "/_layouts/makepdf/makepdfversion.aspx?DocumentName=" + GetFileName(currentItemFileUrl) + "&SourceFolder=" + GetFileName(currentItemEscapedFileUrl) + "&DocumentType=" + docExt + "&RootFolder=" + GetSource() +"')";
strImagePath = ctx.imagesPath + "pdf.gif";
CAMOpt(m, strDisplayText, strAction, strImagePath);
retval = false; // on return, it will continue to add the default items.
break;
case "so on and so on…":
}
return retval;
}
3. Locate the function below:
We need to place the test in an appropriate location within the script, for example
function AddDocLibMenuItems(m, ctx)
{
…
4. Find the statement:
if (currentItemFSObjType != 1)
{
…
(I guess we can have custom menu items for folders, but we mostly want to act on items inside folders…)
5. Insert this code (immediately after if statement above)
Here is where we will call our switch/case to determine whether a custom menu is required. If the menu was added, we will break out and render our custom menu items only.
var retval = AddCustomFolderMenu(currentItemEscapedFileUrl);
if (retval == true)
{
window.onfocus = RefreshOnNextFocus;
return;
// if menu was added, we render and quit.
}
6. Done
Nothing further required… Except for a few folders with the names you have selected. Maybe a browser refresh ;)