Question

i want to hide sharepoint 2010 list/library context menu based on users.

i am using below javascript

  $(document).ready(function(){      
        $('.ms-MenuUIPopupBody').live('blur', function() {       
           var elm = $("div.ms-MenuUIULItem a:contains('View in Browser')"); 
           elm.remove();     
           $("div.ms-MenuUIULItem a:contains('Edit in Browser')").remove();  
         });
  });

It is hiding menu only on focus or blur or mouse-over on the context menu.

the context menu is generating dynamically by core.js file

i need to add the script when the context menu is created.

so i need to find the event for context menu

enter image description here

how to find the click event and add my custom code.

with "Andrew" i found good solution and to handle the context menu event

var _MenuHtc_show;  

function OverrideMenuHtc_show()
{
   _MenuHtc_show = window.MenuHtc_show;
   window.MenuHtc_show= function (m,r,fr,ft,yoff) {
      _MenuHtc_show(m,r,fr,ft,yoff); 
      var elm = $(".ms-MenuUIULItem[text='Alert Me']");
      elm.remove();       
   };
}

ExecuteOrDelayUntilScriptLoaded(OverrideMenuHtc_show, "core.js")

its working fine but its hiding context menu for all webparts. i need to find the context menu selected webpart id to check is it right webpart to hide context menu.

i am able to find the webpart id after once i select webpart with this code.

 var listId = SP.ListOperation.Selection.getSelectedList();  

or

var ctxCur = GetCurrentCtx();
var SPGridViewGuid = ctxCur.listName;

but if i click on "v" symbol with out selecting the webparts its return null.

is there any way to find the context menu selected webpart id. so its solves my problem.

Was it helpful?

Solution

What about that?

 $('.s4-ctx').css('display','none');

It will hide buttons that show context menu.


If you need to hide context menu only from web part with specified id you need to use:

$("div[webpartid='85c13a33-57a3-456b-a19b-c68c97756184'] .s4-ctx").css('display','none');

If you want to remove menu item, for ex. "Alert Me" add this script that overrides standard core.js function "MenuHtc_show" and hides menu item from context menu in specified webpart:

var _MenuHtc_show;  

function OverrideMenuHtc_show()
{
   _MenuHtc_show = window.MenuHtc_show;
   window.MenuHtc_show= function (m,r,fr,ft,yoff) {
      _MenuHtc_show(m,r,fr,ft,yoff); 
      var items = $("div[webpartid='85c13a33-57a3-456b-a19b-c68c97756184'] .ms-MenuUIULItem[text='Alert Me']");
      $(items).css('display','none');       
   };
}

ExecuteOrDelayUntilScriptLoaded(OverrideMenuHtc_show, "core.js")

OTHER TIPS

Any reason why you are not using a HideCustomAction?

Here is the MSDN for that.

And here is a resource for finding the correct groups

finally this solve my problem

i enhanced "Andrew" script to make full functional.

ExecuteOrDelayUntilScriptLoaded(OverrideMenuHtc_show, "core.js");

var _MenuHtc_show;

function OverrideMenuHtc_show() {

    _MenuHtc_show = window.MenuHtc_show;
    window.MenuHtc_show = function (m, r, fr, ft, yoff) {
        _MenuHtc_show(m, r, fr, ft, yoff);

        var ctxCur = listcontext;
    //list or library GUID
        var listid = ctxCur.listName;

    if(listid =='your list\library GUID')
    {

                var elm = $(".ms-MenuUIULItem[text='Alert Me']");
                elm.remove();

    }

    };
}

I wanted to implement the same on SharePoint 2013. The CSS solution only works for a particular page. If one creates a new view this will not work.

@kavalirakesh and @Andrew answer worked partially for me. the var ctxCur = listcontext; was throwing exception on listcontext is undefined. Instead I used GetCurrentCtx() to get ClientContext.

But I was getting the following javascript error when I was clicking on the Setting cog on the top right side of the page. "An error has occurred with the data fetch. Please refresh the page and retry."

My javascript code added through a Delegate Control on the master page head.

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Control Id="AdditionalPageHead"
           Sequence="90"
           ControlSrc="~/_CONTROLTEMPLATES/15/MyCustomFolder/ECBDelegateControl.ascx" />
</Elements>

The user control "ECBDelegateControl.ascx" had the scrip below

<script type="text/javascript"  src="../../../_layouts/15/MyScripts/Script/jquery-3.1.1.min.js"></script>
<script type="text/javascript">
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {
        ExecuteOrDelayUntilScriptLoaded(OverrideMenuHtc_show, "core.js");
    });

    function OverrideMenuHtc_show() {
        var ctx = GetCurrentCtx();
        var listName = ctx.ListTitle;

        if (listName == 'My First List' ||
            listName == 'My Second List') {
            var _MenuHtc_show;
            _MenuHtc_show = window.MenuHtc_show;
            window.MenuHtc_show = function (m, r, fr, ft, yoff) {
                _MenuHtc_show(m, r, fr, ft, yoff);
                var elm = $("li.ms-core-menu-item[text='Delete Item']");
                elm.remove();
            }
        };
    }
</script>

The ClientContext was null and I had to load the sp.js before I can call GetCurrentCtx(). The javascript error I used to get is also gone. This is much better solution than changing core.js file.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top