
Edit Control Block (ECB) is the context menu that is displayed for all items in Lists and Document Libraries. This post focus on editing or hiding existing ECB menu items.
If you want to add new custom actions or menu items to the ECB menu, You can do it by:
1) Using the element <CustomAction Location=”EditControlBlock” > in a Feature [recommended way].
2) Create a javascript function named Custom_AddListMenuItems and add it to a page or master page. For e.g.
//This function is called automatically from core.js in-built functions like AddListMenuItems(), AddDocLibMenuItems() etc... function Custom_AddListMenuItems(m, ctx) { //This function creates a new ECB item. CAMOpt(m, "Test ECB item", "alert(I am created from script!!');", "/_layouts/images/testECB.GIF"); //This function creates a seperator line. CAMSep(m); }
If you want to hide or delete an existing menu item which is provided out-of-box by SharePoint, we cannot do it by using a feature. The out-of-box actions or menu items in ECB are rendered by using a JavaScript file, which is core.js [Note : Modifying the core.js file is not supported or recommended]. So there are two ways we can hide existing menu items :
- Create a copy of core.js and edit it to remove the ECB menu items we don’t want.
- Use JQuery to hide the ECB menu items without editing core.js
Important:Please do not edit existing core.js
Following below steps to create a customcore.js file and refer it in the master page.
- Copy the core.js file from its default location ( For SP2010 : %ProgramFiles%Common FilesMicrosoft Sharedweb server extensions14TEMPLATELAYOUTS1033; For SP2007:%ProgramFiles%Common FilesMicrosoft Sharedweb server extensions12TEMPLATELAYOUTS1033) place it in the same folder, and rename as customcore.js file.
- Remove unwanted ECB menu items by editing the appropriate functions like CreateMenuEx()(for SP2007 core.js), BuildMenu()(for SP2010 core.js), AddVersionMenuItems(), AddDocLibMenuItems() etc. For more details on the functions please see the section ”ECB menu items in core.js” below.
- In the master page, add the following line to render the customcore.js file [Note: core.jsis also required]:
<!--It is important to refer core.js even if you create and refer customcore.js--> <SharePoint:ScriptLink language="javascript" name="core.js" Defer="true" runat="server"/> <SharePoint:ScriptLink language="javascript" name="customcore.js" Defer="true" runat="server"/> - Save and publish the master page.
The JavaScript function CreateMenu() (for SP2007) or BuildMenu() (for SP2010) in core.js creates the menu items when you hover or click to edit a list item.
//This function is used to create ECB menu in SP2010 function BuildMenu(a) { ULSrLq: ; var b = CMenu(currentItemID + "_menu"); if (!b) return; else if (a.isVersions) //Add ECB menu item if versioning is enabled in a list or document library(For eg. "Check Out","Version History") AddVersionMenuItems(b, a); else if (a.listTemplate == 121) //Add ECB menu item for solution catalog AddSolutionsCatalogMenuItems(b, a); else if (a.listBaseType == 1) //Add menu item for items in document library (For eg. "Edit Document") AddDocLibMenuItems(b, a); else if (a.listTemplate == 200) //Add menu item for items in meetings AddMeetingMenuItems(b, a); else //Add menu item for items in standard List (For eg. "View Item","Edit Item") AddListMenuItems(b, a); InsertFeatureMenuItems(b, a); return b } //This function is used to create ECB menu in SP2007 function CreateMenu(e) { if (!IsContextSet()) return; var ctx=currentCtx; if (e==null) e=window.event; var srcElement=e.srcElement ? e.srcElement : e.target; if (itemTable==null || imageCell==null || (onKeyPress==false && (srcElement.tagName=="A" || srcElement.parentNode.tagName=="A"))) return; return CreateMenuEx(ctx, itemTable, e); }
The CreateMenu() function in SP2007 core .js calls CreateMenuEx() which calls respective functions to create ECB menu items for list items in a library, meeting or custom list.
function CreateMenuEx(ctx, container, e)
{
if (container==null)
return;
IsMenuShown=true;
document.body.onclick="";
var m;
m=CMenu(currentItemID+"_menu");
if (!m)
return;
else if (ctx.isVersions)
//Add menu item if versioning is enabled in a list or document library(For eg. "Check Out","Version History")
AddVersionMenuItems(m, ctx);
else if (ctx.listBaseType==1)
//Add menu item for items in document library (For eg. "Edit Document")
AddDocLibMenuItems(m, ctx);
else if (ctx.listTemplate==200)
//Add menu item for items in meetings
AddMeetingMenuItems(m, ctx);
else
//Add menu item for items in standard List (For eg. "View Item","Edit Item")
AddListMenuItems(m, ctx);
InsertFeatureMenuItems(m, ctx);
currentEditMenu=m;
container.onmouseout=null;
OMenu(m, container, null, null, -1);
itemTable=GetSelectedElement(container, "TABLE");
m._onDestroy=OutItem;
e.cancelBubble=true;
return false;
}
We can edit the JS functions(and inner functions) called above to remove the ECB menu items.We can comment the lines of code which create menu items. These lines are :
menuOption=CAMOpt(m, strDisplayText, strAction, strImagePath, null, 700);
menuOption.id="ID_[**action**]";
The CAMOpt() function creates a new ECB menu item.We can comment the lines which call it to create a specific ECB menu item. Below are few examples. To remove “Check Out” ECB menu item, we can comment the below lines in AddCheckinCheckoutMenuItem() function:
// menuOption=CAMOpt(m, strDisplayText, strAction, strImagePath, null, 700); // menuOption.id="ID_Checkout";
To remove “Delete” ECB menu item from a list item, we can comment the below lines in AddListMenuItems() function:
//menuOption=CAMOpt(m, strDisplayText, strAction, strImagePath, null, 300); //menuOption.id="ID_DeleteItem";
Use JQuery to hide the ECB menu items
The ECB menu html which is created on fly by CreateMenu() looks like below. This can be manipulated by JQuery as well.
<!--the menu id is formed by Item Id + "_menu" -->
<menu id="268_menu" class="ms-SrvMenuUI">
<span type="option" text="View Properties" onmenuclick="STSNavigate('/Documents/Forms/DispForm.aspx?ID=268')" sequence="200" id="ID_ViewProperties"></span>
<span type="option" text="Edit Properties" onmenuclick="STSNavigateWithCheckoutAlert('/Documents/Forms/EditForm.aspx?ID=268',1,'0','u002fDocumentstest.pdf','http://sp2007site')" iconsrc="/_layouts/images/edititem.gif" iconalttext="" sequence="220" id="ID_EditProperties"></span>
<span type="option" text="Manage Permissions" onmenuclick="NavigateToManagePermsPage('http://sp2007site', '{A8073644-42BB-46C2-9CEC-33371585C51C}','268')" iconsrc="/_layouts/images/manageperm.gif" iconalttext="" sequence="250" id="ID_MngPerms"></span>
<span type="option" text="Delete" onmenuclick="DeleteDocLibItem('/_vti_bin/owssvr.dll?CS=65001&Cmd=Deletesequence="310" id="ID_DeleteDocItem"></span>
<span type="submenu" text="Send To" iconalttext="" sequence="400" id="ID_Send"><span type="option" text="Other Location" onmenuclick="STSNavigate('http://sp2007site/_layouts/copy.aspx?SourceUrl=%2FDocuments%2F090505%2Dtest%2Epdf&')" iconsrc="/_layouts/images/sendOtherLoc.gif" iconalttext="" id="ID_OtherLocation"></span>
<span type="separator"></span>
<span type="option" text="E-mail a Link" onmenuclick="javascript:navigateMailToLinkNew('http://sp2007site/Documents/090505-test.pdf')" iconsrc="/_layouts/images/gmailnew.gif" iconalttext="" id="ID_SendToEmail"></span>
<span type="option" text="Create Document Workspace" onmenuclick="STSNavigate('http://sp2007site/_layouts/createws.aspx?list={A8073644-42BB-46C2-9CEC-33371585C51C}&item=268&RootFolder=%2FDocuments')" iconalttext="" sequence="1140" id="ID_CreateDWS"></span>
<span type="option" text="Download a Copy" onmenuclick="STSNavigate('http://sp2007site/_layouts/download.aspx?SourceUrl=%2FDocuments%2F090505%2Dtest%2Epdf&') iconalttext="" id="ID_DownloadACopy"></span>
</span>
<span type="separator"></span>
<span type="option" text="Check Out" onmenuclick="CheckoutDocument('http://sp2007site', '%2FDocuments%2F090505%2Dtest%2Epdf', '.3')" iconsrc="/_layouts/images/checkout.gif" iconalttext="" sequence="700" id="ID_Checkout"></span>
<span type="option" text="Version History" onmenuclick="NavigateToVersionsAspx('http://sp2007site', 'list={A8073644-42BB-46C2-9CEC-33371585C51C}&ID=268&FileName=%2FDocuments%2F090505%2Dtest%2Epdf')" iconsrc="/_layouts/images/versions.gif" iconalttext="" sequence="800" id="ID_Versions"></span>
<span type="option" text="Workflows" onmenuclick="STSNavigate('http://sp2007site/_layouts/Workflow.aspx?ID=268&List={A8073644-42BB-46C2-9CEC-33371585C51C}&Source=http%3A%2F%2Fsp2007site%2FDocuments%2FForms%2FAllItems%2Easpx')" iconsrc="/_layouts/images/workflows.gif" iconalttext="" sequence="900" id="ID_Workflows"></span>
<span type="separator"></span>
<span type="option" text="Alert Me" onmenuclick="NavigateToSubNewAspx('http://sp2007site', 'List={A8073644-42BB-46C2-9CEC-33371585C51C}&ID=268')" iconalttext="" sequence="1100" id="ID_Subscribe"></span>
<span type="separator"></span>
</menu>
The below jquery snippet hides the “Check Out” ECB menu item at mouseover on the div(class=ms-MenuUIPopupBody) that is created on fly everytime a ECB menu is constructed.
$(document).ready(function(){
$('.ms-MenuUIPopupBody').live('mouseover', function() {
$('#ID_Checkout').parent().hide();
$('#ID_Checkout').remove();
});
});
The jQuery .live event is not fired until after the menu is created AND displayed, so the items on the menu are seen first, then disappear. Is this the solution you meant to provide using jQuery? Is there another method in jQuery that would be more suited to stripping these items out prior to the menu being show to the end user?
Jquery to hide “Edit Item” from sharepoint list ?? pls share coding
The last jquery code wriiten at the end of the post is what you need. Please note that using jquery menu will hide on first hover only.
I am using the following in a document library. On the all items page added via a CEW
$(document).ready(function(){
$(‘.ms-MenuUIPopupBody’).live(‘mouseover’, function() {
$(‘#ID_Checkout’).parent().hide();
$(‘#ID_Checkout’).remove();
});
});
Does absolutely nothing! What is wrong here? Nothing is hiding! I really want to hide the Approve/Reject if possible, no modifications to Core.js want to use jquery
The first option worked for me. Thanks Amit!!
Thanks, it helped me to hide sub menu within Send To option.
Hi Amit,
How to redirect “Add Document” and “Upload Document” button in the ribbon to custom upload document page.
ANy help would be apprcited…
Thanks,
Rohidas
This link is about MOSS but I think it holds true for SP2010 as well :
http://msdn.microsoft.com/en-us/library/cc713554.aspx
If you already developed a custom upload page, you can just follow section “Redirecting the User to the New Upload Page” in above link.
BTW, your question is good one, Can you please add it here so that it may be easily searchable and helpful for other users :
http://techforum.extreme-sharepoint.com
Thanks Amit…
I implemented it..But it is not working in 2010. However i am able to modify ribbon button but how to modify the “Add Document” link so that it will redirect to custom upload page. And every time i will create the document this change should reflect.
You can also try a hack using jquery. The ‘add document’ links look like below:
<a class=”ms-addnew” id=”idHomePageNewDocument” href=”/_layouts/Upload.aspx” onclick=”" target=”_self” rel=”nofollow”>Add document </a>
In default master page, you can have
$(“.ms-addnew”).attr(“href”, “/pages/customupload.aspx”);
$(“.ms-addnew”).click(function(){ });
Hi Amit,
I want to know if the triggered ECB is associated to folder object or a list item. Is it possible to check that using parameters ctx or m? Or perhaps another means?
Cheers,
Mike
This option worked for me but I want to hide option before Menu get loaded.
to hide ecb menu
$(document).ready(function(){
$(‘.ms-MenuUIPopupBody’).live(‘mouseover’, function() {
$(‘#ID_Checkout’).parent().hide();
$(‘#ID_Checkout’).remove();
});
});
is good solution, is there any way to hide for particular webpart?