Friday 19 June 2015

Show 'Site Actions' ribbon menu only for SP Group members in SharePoint 2010

Step 1:
Create a SharePoint group 'SiteAdmins' at Site Collection level with Design and Contribute access

Step 2:
Add\Upload below 2 JS files to SiteAssets of root site
  1. jquery.SPServices-0.7.2.min.js
  2. jquery-1.8.3.min.js
Step 3: 
Refer above 2 JS in Master file.
Edit default Master page, find ‘<asp:ScriptManager’ under Form tag and replace(or include the script references) to include JQuery and SPServices scripts

 <asp:ScriptManager id="ScriptManager" runat="server" EnablePageMethods="false" EnablePartialRendering="true" EnableScriptGlobalization="false" EnableScriptLocalization="true" >
  <Scripts>                             
                                <asp:ScriptReference Path="<%$SPUrl:~SiteCollection/SiteAssets/jquery-1.8.3.min.js%>"></asp:ScriptReference>                         
                                <asp:ScriptReference Path="<%$SPUrl:~SiteCollection/SiteAssets/jquery.SPServices-0.7.2.min.js%>"></asp:ScriptReference>                               
                </Scripts>             
</asp:ScriptManager>

Step 4:
Hide ‘Site Actions’ and top ribbon for all users by default.
<!--<div id="s4-ribbonrow" class="s4-pr s4-ribbonrowhidetitle">-->
<div id="s4-ribbonrow" style="display: none;">

Step 5:
Include below script block before between </Body> and </HTML> to check if current user is member of 'SiteAdmins' group and display if yes.

<script type="text/javascript">
$(document).ready(function() {$().SPServices({
      operation: "GetGroupCollectionFromUser",
      userLoginName: $().SPServices.SPGetCurrentUser(),
      async: false,
      completefunc: function(xData, Status) {
        if($(xData.responseXML).find("Group[Name='SiteAdmins']").length == 1) {
$('#s4-ribbonrow').show();
}
      }
   });  
   });
</script>

Step 6 (Optional): If you want to display 'Welcome <current user>',  add wssuc:Welcome control (copy & paste) to some other location other than ribbon. 

Thats all, the Ribbon will be hidden for all users except for 'SiteAdmins' members. They have access to 'Site Actions' menu.

No comments: