Thursday, April 29, 2010

Add/Hide Menu Item in the ECB from SharePoint Items

You add a new custom action to items the Edit Control Block (ECB) menu by using the Features. However, you can not hide a item from the ECB by using the features. But you can use the JavaScript to add or a new item to the ECB.

There is file named core.js in the<%Program files %>\common files\Microsoft shared\web server extensions\12\Template\layouts\1033 which is responsible for rendering.

Add a new item
1.Add a content editor web part to the page.(If you want all the lists and document libraries have the same feature, you can add the script to your master page. )

2.Add the script to the web part(The code snippet came from here)


function Custom_AddDocLibMenuItems(m, ctx) 
{ 
    var strDisplayText = Your display text 
    var strAction = Your action 
    var strImagePath = Your image path   
    // Add our new menu item 
     CAMOpt(m, strDisplayText, strAction, strImagePath);   
    // add a separator to the menu 
     CAMSep(m);  
    // false means that the standard menu items should also be rendered 
    return false; 
}

Hide a item

If you just want to hide menu item in a single list/library, add a content editor like above.

There is a function named AddDocLibMenuItems in the core.js that is used to render the ECB. If can find there are some functions calling in the AddDocLibMenuItems like below

AddCheckinCheckoutMenuItem — Add Check in/out menu item
AddVersionsMenuItem — Add version check menu item
AddSendSubMenu — Add send to menu item

So, if you want to hide the item from ECB, the easiest way is to comment the corresponding function in the AddDocLibMenuItems. But you should not midofy the core.js in the server directly, which would impact the whole farm. If you check the AddDocLibMenuItems, you will find that it would check if the Custom_AddDocLibMenuItems exist. So you can add your Custom_AddDocLibMenuItems function in the content editor, and return true.(Returning true would prevent from calling the default AddDocLibMenuItems).

The only thing you need do is to create a Custom_AddDocLibMenuItems function in the content editor web part, except for this part. If you include this part, it would throw a stack overflow exception. Because it cause a infinite recursive calling.


if (typeof(Custom_AddDocLibMenuItems) !="undefined") 

{ 

    if (Custom_AddDocLibMenuItems(m, ctx)) 

    return; 

}



In my example, I want to hide the menu item–Edit in Microsoft InfoPath. So,I delete two CAMOpt functions in the code. You can find these two lines below.
if (ctx.isWebEditorPreview==0 && ctx.listBaseType==1)
       
{
           
if (ctx.listTemplate==109 && itemTable.getAttribute("IsImage")=="1")
           
{
                strDisplayText
=L_EditInOIS_Text;
                strAction
="EditSingleImage('"+currentItemID+"')";
                strImagePath
=ctx.imagesPath+"oisweb.gif";
                menuOption
=CAMOpt(m, strDisplayText, strAction, strImagePath, null, 240);
                menuOption
.id="ID_EditInOIS";
           
}
           
else
           
{
                setDocType
();
               
if (currentItemAppName !="" && currentItemOpenControl !="")
               
{
              strDisplayText
="";
             
if (currentItemAppName !=" ")
                        strDisplayText
=StBuildParam(L_EditIn_Text, currentItemAppName);
                   
else
           
{
                 
var   objEditor=StsOpenEnsureEx(currentItemOpenControl+".3");
                     
if (objEditor !=null )
                    strDisplayText
=L_EditInApplication_Text;
                       
}
           
if (strDisplayText !="")
           
{
                strAction
="editDocumentWithProgID2('"+currentItemFileUrl+"', '"+currentItemProgId+"', '"
+currentItemOpenControl+"', '"+bIsCheckout+"', '"+ctx.HttpRoot+"', '"+currentItemCheckedoutToLocal+"')";
                            strImagePath
=ctx.imagesPath+currentItemIcon;
                            menuOption
=CAMOpt(m, strDisplayText, strAction, strImagePath, null, 240);
                            menuOption
.id="ID_EditIn_"+currentItemAppName;
           
}
               
}
           
}
       
}



No comments:

Post a Comment