1

I would like to hide the gear button for non admins users of my SharePoint website. And to perform, I don't want to modify code from my masterPage (i am not allowed to do this). But I can create a copy of it, a kind of Custom MasterPage but I do not know what code to put on it to hide the gear button ONLY for non admin user. I was thinking about permission level but i do not know which on to put on (or off) to avoid users (even contributor user) to get this gear button. Thanks for your precious help!

1
  • 1
    you can't unless you add a css on every site page, wikipage and listview page some CSS to hide the Site Actions button
    – Mike
    May 30 '18 at 20:09
2

Finally I get it!!

Step 1: I have duplicated my masterpage in a custom masterpage. To do so :

Gear button/site setting/Web Designer Galleries Part/Master Pages

  • find the master page by default (mine was seattle.master)
  • click on the narrow and choose :Download a copy
  • on my PC I renamed it as custom_seattle.master" and uploaded it in the same gallery.
  • on the metadata when uploaded, put in Type content : ASP NET MasterPage

Step 2: In SharePoint Designer, in Master Page list, find your custom_seatle.master page, click check out and advanced modify. In the page code, find : MenuItemTemplate (it is about 8 occurrences) and in the attributes, find PermissionsString and replace value(s) by "FullMask" and find PermissionMode and replace value by "All" example :

<SharePoint:MenuItemTemplate runat="server" ID="MenuItem_ViewAllSiteContents"
    Text="<%$Resources:wss,quiklnch_allcontent_15%>"
    Description="<%$Resources:wss,siteactions_allcontentdescription%>"
    ImageUrl="/_layouts/15/images/allcontent32.png?rev=23"
    MenuGroupId="200"
    Sequence="240"
    UseShortId="true"
    ClientOnClickNavigateUrl="~siteLayouts/viewlsts.aspx"
    PermissionsString="FullMask"
    PermissionMode="All" />

Save your page, check in by put publish principal version.

Step 3: in your SharePoint Site in browser, go to this page: /_layouts/15/changesitemasterpage.aspx (example : http://mySpWebSite/_layouts/15/changesitemasterpage.aspx) And in the properties list, choose your custom page (custom_seatle.master)

And it is OK!!! :)

1
  • How can we hide the gear icon in HTML Master page? It seems MenuItem is not existing in HTML master page.
    – Mark L
    Jul 26 '19 at 2:35
1

Are you allowed to add Custom Actions?

if so then you could use this code here:

(function getCustomAction() {
    this.clientContext = new SP.ClientContext();
    var oWebsite = clientContext.get_web();
    this.collUserCustomAction = oWebsite.get_userCustomActions();
    clientContext.load(oWebsite, 'UserCustomActions', 'Title');
    clientContext.executeQueryAsync(Function.createDelegate(this, this.enumerateCustomActions), Function.createDelegate(this, this.onQueryFailed));
})()

function enumerateCustomActions() {
    var customActionEnumerator = collUserCustomAction.getEnumerator();
    var exists = false;
    while (customActionEnumerator.moveNext()) {
        var oUserCustomAction = customActionEnumerator.get_current();
        if (oUserCustomAction.get_title() == 'HideGearIcon') {
            exists = true;
            oUserCustomAction.deleteObject();
            clientContext.load(oUserCustomAction);
            clientContext.executeQueryAsync(Function.createDelegate(this, this.onDeleteQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
            break;
        }
    }
    if (!exists) {
        oUserCustomAction = collUserCustomAction.add();
        oUserCustomAction.set_location('ScriptLink');
        oUserCustomAction.set_sequence('1000');
        oUserCustomAction.set_title('HideGearIcon');
        oUserCustomAction.set_scriptBlock(`\
            document.write(\'\
                \\<script\\>\
                    SP.SOD.executeFunc("sp.js", "SP.ClientContext", checkPermissions);\
                    function checkPermissions (){\
                        var ctx = new SP.ClientContext.get_current();\
                        var web = ctx.get_web();\
                        var ob = new SP.BasePermissions();\
                        ob.set(SP.PermissionKind.manageWeb);\
                        var perm = web.doesUserHavePermissions(ob);\
                        ctx.executeQueryAsync(\
                            function () {\
                                if(!perm.get_value()){\
                                    addCSS("#O365_MainLink_Settings {display:none !important}");\
                                }\
                            }, function (a, b) {\
                                addCSS("#O365_MainLink_Settings {display:none !important}");\
                            }\
                        );\
                    };\
                    function addCSS(css){\
                        var head = document.getElementsByTagName("head")[0];\
                        var s = document.createElement("style");\
                        s.setAttribute("type", "text/css");\
                        if (s.styleSheet) {\
                            s.styleSheet.cssText = css;\
                        } else {\
                            s.appendChild(document.createTextNode(css));\
                        };\
                        head.appendChild(s);\
                    }\
                \\</script\\>\
            \');\
        `);
        oUserCustomAction.update();
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onDeployQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
    }
}

function onDeleteQuerySucceeded() {
    alert('CustomAction removed');
}

function onDeployQuerySucceeded() {
    alert('CustomAction deployed');
}

function onQueryFailed(sender, args) {
    alert('Something went wrong: ' + args.get_message() + '\n' + args.get_stackTrace());
}

This code will deploy a Custom Action which checks the users permission and injects some CSS into the page if the user does not have the ManageWeb permission. The code that actually deploys the Custom Action is wrapped in another function that allows you to toggle the Custom Action on and off. Execute this code in the console of your browser and the Custom Action will be deployed. Execute it again and the Custom Action will be removed.

The code contains Template Literals so your browser needs to be ES6-compliant.

3
  • Thx daniel.But When I entry this code in my browser console it put me "incorrect character". And I cannot see in which line...
    – Dany
    Jun 2 '18 at 13:33
  • Which browser are you using? Have you tried another browser? For me the code works in Firefox 60.0.1 (64-bit) and Chrome 66.0.3359.181 (Official Build) (64-bit) Jun 2 '18 at 15:32
  • I am using IE version 11.0.10240.17831. Thx Daniel for your help. I found another way to get it :)
    – Dany
    Jun 2 '18 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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