I am using a code for hiding site content items for users except admin:

The below script is used to check if user is admin but currentUser.get_isSiteAdmin() always gives True:

Code is below:

<script type="text/javascript">

function getUserInfo()

    this.clientContext = new SP.ClientContext.get_current();
    this.oWeb = clientContext.get_web();
    currentUser = this.oWeb.get_currentUser();
    this.clientContext.executeQueryAsync(Function.createDelegate(this,this.onQuerySucceeded), Function.createDelegate(this,this.onQueryFailed));
function onQuerySucceeded() 
  var user= currentUser.get_isSiteAdmin();
  if(user == false)
    alert("user is not admin")

  alert("user is admin")


function onQueryFailed(sender, args)
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());


You don't need to do any user checking yourself (if you have the Admins in a SharePoint Group)

Do it the other way around.

  • Hide webparts by default

  • Make them visible for Admins,
    code in a CEWP with a Target Audience so it only executes for the Admin-Group

This will also not flash/then hide contents

| improve this answer | |
  • I want to hide list/libraries from site contents. All users have access to these libraries but they should visible to admin only. But while checking the admin it always gives every user as admin. Which is not the case. – Arun Bhandari Jun 16 '16 at 13:11
  • Ah.. SiteContents is a systems page where you can't add webparts, then this method won't work. – Danny '365CSI' Engelman Jun 17 '16 at 9:41
  • But we can hide the 'site content' items from a normal page with content editor web part having JavaScript reference. – Arun Bhandari Jun 17 '16 at 13:04

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.