SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a tutorial or specific bit of code I can use to hide the ribbon for all users except admins or a user group?

This needs to be for sharepoint 2013 (rather than 2010)

Can anyone help please?

share|improve this question
    
    
Online or on prem? – Atish Dipongkor Jan 12 at 9:24
    
hi Atish Dipongkor, its intranet/internal – uatonly Jan 12 at 9:28
    
Hi user19952 trying that now, the part about importing my own files in the design manager might be an issue - they are currently in a sub folder – uatonly Jan 12 at 9:29
up vote 10 down vote accepted

CSS + JS Solution

Add following CSS in your master page

#RibbonContainer{
  display: none;
}

Now GET current users details

/_api/web/currentuser?$expand=Groups

Make a GET request to the above end-point, it will return current user's details with his/her Groups

Now check if current user IsSiteAdmin, then display Ribbon again.

if (response.d.IsSiteAdmin){
   document.querySelector("#RibbonContainer").style.display = 'block';
}

If you need to check if current user exists in particular Group, then

var groupName = "Your Group Name";

var isUserExistsInGroup = response.d.Groups.results.some(function(g) {
    return g.Title == groupName;
});

if (isUserExistsInGroup) {
    document.querySelector("#RibbonContainer").style.display = 'block';
}

Full JS Code put it in your master page

(function() {
    var xmlhttp = new XMLHttpRequest();

    xmlhttp.open("GET", _spPageContextInfo.webAbsoluteUrl + '/_api/web/currentuser?$expand=Groups');

    xmlhttp.setRequestHeader("Accept", "application/json;odata=verbose");

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == XMLHttpRequest.DONE) {
            if (xmlhttp.status == 200) {
                var response = xmlhttp.responseText;

                if (response.d.IsSiteAdmin) {
                    document.querySelector("#RibbonContainer").style.display = 'block';
                }

                var groupName = "Your Group Name"; //Give here your Group Name

                var isUserExistsInGroup = response.d.Groups.results.some(function(g) {
                    return g.Title == groupName;
                });

                if (isUserExistsInGroup) {
                    document.querySelector("#RibbonContainer").style.display = 'block';
                }

            } else {
                alert('Error: ' + xmlhttp.statusText)
            }
        }
    }

    xmlhttp.send();

})();
share|improve this answer
    
Thank you @Atish, i'm trying this. I have added the CSS snipet in the master css file second JS snippet to JS file - I then amended the group name. Its not currently working - have i missed something? do these all need to be added to the masterfile? Thank you for your help – uatonly Jan 12 at 11:19
    
Script also should be in Master page. So me your code. Do you have jquery in you master page – Atish Dipongkor Jan 12 at 11:23

How about Security Trimming that Ribbon in the masterpage?

<!--CS: Start Security Trim Snippet-->
    <!--SPM:<%@Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"%>-->
    <!--MS:<SharePoint:SPSecurityTrimmedControl runat="server" AuthenticationRestrictions="AuthenticatedUsersOnly" Permissions="AddAndCustomizePages" PermissionContext="RootSite">-->
        <!--PS: Start of READ-ONLY PREVIEW (do not modify)--><span><!--PE: End of READ-ONLY PREVIEW-->
            <div id="s4-ribbonrow" class="s4-pr s4-ribbonrowhidetitle">
            ...
            </div>
        <!--PS: Start of READ-ONLY PREVIEW (do not modify)--></span><!--PE: End of READ-ONLY PREVIEW-->
    <!--ME:</SharePoint:SPSecurityTrimmedControl>-->
    <!--CE: End Security Trim Snippet-->

MSDN Reference:

https://msdn.microsoft.com/en-us/library/office/jj822366.aspx

share|improve this answer
    
thank you Atish Dipongkor this works – uatonly Jan 13 at 11:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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