0

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.load(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")

  }
  else
  {
  alert("user is admin")

  }

}
function onQueryFailed(sender, args)
{
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
ExecuteOrDelayUntilScriptLoaded(getUserInfo,"sp.js");

</script>
1

1 Answer 1

0

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

3
  • 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. Jun 16, 2016 at 13:11
  • Ah.. SiteContents is a systems page where you can't add webparts, then this method won't work. Jun 17, 2016 at 9:41
  • But we can hide the 'site content' items from a normal page with content editor web part having JavaScript reference. Jun 17, 2016 at 13:04

Your Answer

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

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