Hiding Menu Items in the ECB from SharePoint List Items

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
We will discuss both the ways below ( I personally prefer no:2 as it is much easier )
 
Copy and Edit 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.

  1. 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.
  2. 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.
  3. 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"/>
  4. 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&amp;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&amp;')" 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}&amp;item=268&amp;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&amp;') 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}&amp;ID=268&amp;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&amp;List={A8073644-42BB-46C2-9CEC-33371585C51C}&amp;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}&amp;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();         

         }); 

});
17 Responses to “Hiding Menu Items in the ECB from SharePoint List Items”
  1. Dodge says:

    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?

  2. Aneesh says:

    Jquery to hide “Edit Item” from sharepoint list ?? pls share coding

    • Amit Kumawat says:

      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.

    • Fran says:

      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

  3. jalsql says:

    The first option worked for me. Thanks Amit!!

  4. nadim Sayyed says:

    Thanks, it helped me to hide sub menu within Send To option.

  5. Rohidas says:

    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

  6. Rohidas says:

    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.

    • Amit Kumawat says:

      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(){ });

  7. Mike says:

    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

  8. Shivaji says:

    This option worked for me but I want to hide option before Menu get loaded.

  9. rakesh says:

    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?

  10. Jay says:

    I took a different approach. I changed the Perm attribute. This avoids the problem of the mouseover. Here is the complete code which also accomodates for Grouping.

    $(document).ready(function () {
    //remove edit item in context menu
    var contextTBL = $(“table[onmouseover$='OnItem(this)']“);

    //this following is used when the list view has grouping
    $(“a[onclick^='javascript:ExpCollGroup']“).each(
    function () {

    var onlickAttr = $(this).attr(“onclick”).replace(“return false;”, “”);
    $(this).attr(“onclick”, “”);

    $(this).off(“click”);
    $(this).click(function () {
    eval(onlickAttr);

    setTimeout(function () { RemoveMenuItems(contextTBL) }, 1000);

    }); //click
    } //function
    ); //each

    RemoveMenuItems(contextTBL);

    }); //document.ready

    function RemoveMenuItems(contextTBL) {

    contextTBL.each(
    function () {
    $(this).attr(“Perm”, “0xb008431063″) //View Item, Version History, Alert Me
    });

    }

  11. Gman says:

    Amit & Jay,

    Thanks for these solutions. Unfortunately neither appears to work in SP2013 using the latest jQuery (1.10.2). I’ve checked that 2013 is still using the BuildMenu() function like 2010. Curious if either of you have tried this again for SP2013.

    Regards

  12. Sharepoint_C. says:

    Hi there,
    I’m trying to find the ID for the ECB list item – E-mail as attachment. Has anyone found it or can point me in the right direction?

    Thanks.

  13. Prasad says:

    Hello Amit,

    I have a requirement for one of my project is to add one button in ECB menu of Version History page. On click of this button “Delete” button will get disabled.

    Could you pleae put some light on this? … how to achive this? ….

    Regards,
    Prasad

Leave a Reply

Subscribe

Get every post delivered to your inbox via FeedBurner :

© 2010-2013 Extreme Sharepoint | The content is copyrighted to Amit Kumawat and may not be reproduced on other websites.