1

I would like to list all users that has been given permission to site, but when I use this;

   var clientContext = SP.ClientContext.get_current();
   var web = clientContext.get_web();
   var users = web.get_siteUsers();
   clientContext.load(users);

It somehow list all users that is on the whole site collection, and not only those who has permission on current site.

1 Answer 1

2

To retrieve users that has been given permission to web site you could utilize the following script:

var ctx = SP.ClientContext.get_current();
var assignments = ctx.get_web().get_roleAssignments();
ctx.load(assignments,'Include(Member)'); 
ctx.executeQueryAsync(
     function(){
        var users = assignments.get_data().map(function(a){
            if(a.get_member().get_principalType() == SP.Utilities.PrincipalType.user)
               return a.get_member();    
        });

        //print results
        for(var i = 0; i < users.length; i++){
           console.log(users[i].get_title());
        }

     },
     function(sender,args){
        console.log(args.get_message()); 
     });
2
  • Do you know how I can get Permission Levels of each users?
    – williamwmy
    Commented Nov 23, 2016 at 9:48
  • 1
    Nevermind, I found that I could use: get_roleDefinitionBindings().getItemAtIndex(0).get_name()
    – williamwmy
    Commented Nov 23, 2016 at 12:32

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.