2

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.

5
  • Possible duplicate to: sharepoint.stackexchange.com/questions/66998/… Apr 30, 2013 at 9:39
  • The context menu is security trimmed and only shows items based on permissions of the current user. Why do you want to hide it? It is just as easy to create a view that does not use this item and restrict some users to that view, but what business case are you trying to solve? Apr 30, 2013 at 10:42
  • my client want to hide the ribbon buttons based on users or groups (not using list/library permissions), i am able to hide ribbon buttons. i am using javascript to hide the ribbon buttons but in context menu the option is still available so i need to hide in context menu also. ex:- if i hide the "alert me" in ribbon, the option is still available in context menu. so i want to extend the webpart to hide context menu also. Apr 30, 2013 at 10:55
  • i am using ExecuteOrDelayUntilScriptLoaded(function () { SP.UI.Workspace.add_resized(RibbonresizeHandler); }, "SP.js"); function RibbonresizeHandler() { // do some action } to handle the ribbon. it will raise when ever the ribbon bar show or hide, i need to know some functionality to with context menu Apr 30, 2013 at 10:56
  • Please don't post the same question more than once. Edit your original question if you need to expand it. Thanks. [mod]
    – SPDoctor
    Apr 30, 2013 at 13:55

4 Answers 4

4

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")
8
  • it will hide context menu ("v") symbol so the full context menu not accessible. but i want hide only few context menu like "alert me", "edit properties" based on uses. i can add css to hide context menu like ".ms-MenuUIULItem[text='Edit Properties']{display:none !important;} " but the problem is it will hide context menu in entire page. if page contains 2 webparts it will applicable to all webparts in page. i want to hide context menu for individual webpart. so when i click ("v") symbol i need to identify the webpart guid and apply css with jquery. Apr 30, 2013 at 10:41
  • If you want to apply changes only to Web Part with specified id you could find Web Part's container using selector "div[webpartid='85c13a33-57a3-456b-a19b-c68c97756184']" and then apply your css to its child elements. I updated my answer. Hope it will help.
    – Andrew
    Apr 30, 2013 at 20:55
  • i understand the concept.this code hides the entire context menu. if you don't mind can you give me a small example to hide a child node of context menu. like can you give me a example to hide "alert me". it will very help full to me. May 1, 2013 at 8:35
  • I updated my answer. Is it fit for you?
    – Andrew
    May 1, 2013 at 10:16
  • excellent script, i checked the script with my webpartid, its raising when ever i chick the "v" symbol, but unfortunately its not hiding the "Alert Me". May 1, 2013 at 11:16
2

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

3
  • i want disable some functionalists to the users dynamically. like "alert me", "edit in browser" even though customer have permissions (exceptional users). i am able to hide ribbon buttons programmatically but in context menu the functionality is still available. i don't want remove his permissions but just hide the ribbon buttons and context menu. Apr 29, 2013 at 9:23
  • So to some users bu not all users? Apr 29, 2013 at 9:24
  • exactly i need to show to some users hide to some users but dynamically. i am using user control as delegate control and adding the javascript dynamically so based on javascript the menu items will hide. i will extend the code to work based on users but in javascript the menu is hiding on mouse hove,but i need when open context menu it has to be hide. Apr 29, 2013 at 9:30
2

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

    }

    };
}
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.