0

How could I count how many users are in Group? How could I count by JavaScript.

2

You can use below code to find that information

var clientContext = new SP.ClientContext();
//Get all groups in site
var groupCollection = clientContext.get_web().get_siteGroups();
// Get the group by name
group = groupCollection.getByName('GroupName');
//Get all SP Users in SP Group
var userCollection = group.get_users();
clientContext.load(userCollection);  
//Execute Query
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed)); 

function onQuerySucceeded() {
    alert(userCollection.get_count());
    $('#divId').html(userCollection.get_count());
}

function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
  • Thansk but i dont see any numbers when I pasted it to the site – Grzegorz Z Jan 30 '15 at 14:10
  • How could i put the result to div ? – Grzegorz Z Jan 30 '15 at 14:11
  • 1
    I have updated code. You should change #divId – Amal Hashim Jan 30 '15 at 14:16
  • Thanks but nothing shown. I tried before show it in alert box, but it show me undefinied. – Grzegorz Z Jan 30 '15 at 14:30
  • 1
    I don't think length is a valid property for user collection. You can get rid of the undefined error on the above code if you add this line clientContext.load(groupCollection); Still you will get an error on length property – Manu Jan 30 '15 at 15:21
0

The code below will do the job but it isn't the best way to achieve this, its old school. You have to change GroupName with the name of your Group

function UserCount() {

var clientContext = new SP.ClientContext.get_current();
var collGroup = clientContext.get_web().get_siteGroups();
var oGroup = collGroup.getByName('GroupName');
this.collUser = oGroup.get_users();
clientContext.load(collUser);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));

}

function onQuerySucceeded() {

var count = 0;
var userEnumerator = collUser.getEnumerator();
while (userEnumerator.moveNext()) {        
    count = count + 1;
}
alert(count);

}

function onQueryFailed(sender, args) {

alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());

}

Reference: How to Work with Users and Groups using JSOM

  • Manu nothing shown :(in alert too – Grzegorz Z Jan 30 '15 at 16:09
  • Are you sure you are referencing the js libraries? – Manu Feb 1 '15 at 2:02
  • How could I finished out the referencing ? – Grzegorz Z Feb 1 '15 at 6:06
  • There are different ways to do it, you could reference the script files in a script editor webpart on the page or if it is needed in multiple pages refer it in Master Page. <script src="/_layouts/15/SP.Runtime.js" type="text/javascript"></script> <script src="/_layouts/15/SP.js" type="text/javascript"></script> – Manu Feb 4 '15 at 10:10
0

If you are using SharePoint 2010, you won't be able to access a group directly by its name; the SP.GroupCollection.getByName JavaScript function was only added in SharePoint 2013.

The alternative is to access the group by its ID (which you can determine by checking the value of the query string parameter "MembershipGroupId" in the address bar when looking at the group's membership page), or to iterate through each of the groups on the site until you find the one you're looking for.

Here's a quick-and-dirty modification of Amal's code that should work on SharePoint 2010:

var groupId = 79; // Enter the actual group ID here!
var clientContext = new SP.ClientContext();
//Get all groups in site
var groupCollection = clientContext.get_web().get_siteGroups();
// Get the group by ID
group = groupCollection.getById(groupId);
//Get all SP Users in SP Group
var userCollection = group.get_users();
clientContext.load(userCollection);  
//Execute Query
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed)); 

function onQuerySucceeded() {
    alert(userCollection.get_count());
    document.getElementById("divId").innerHTML = userCollection.get_count();
}

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

Replace 79 with the actual ID of your group, and replace "divId" with the ID of a div where you want to insert the membership count.

If you absolutely need to grab the group by its name, you can iterate through all the groups and check the Title property of each one, as in the code below.

<script>
ExecuteOrDelayUntilScriptLoaded(function(){
var desiredGroupName = "My Group Name"; // Enter the actual group name here!
    var clientContext = new SP.ClientContext();
    //Get all groups in site
    var groupCollection = clientContext.get_web().get_siteGroups();
    clientContext.load(groupCollection,"Include(Title,Users)");  
    //Execute Query
    clientContext.executeQueryAsync(
        function (){
            var groupEnum = groupCollection.getEnumerator();  
            var groupFound = false;
            var count = 0;
            while(!groupFound && groupEnum.moveNext()){
                count +=1;
                var group = groupEnum.get_current();
                if(group.get_title() == desiredGroupName){
                    groupFound = true;
                    alert("" + desiredGroupName + " has " + group.get_users().get_count()+ " members.");
                    // Un-comment the line below to show the group count in a div with an ID of "divId"
                    // document.getElementById("#divId").innerHTML = group.get_users().get_count();
                }
            }
            if(!groupFound){
                alert("Could not find a group named "+desiredGroupName +"\r\nChecked "+count+" site groups.");
            }
        }, 
        function (sender, args) {
                alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }
    );  
},"SP.JS");
</script>

Replace "My Group Name" with the actual name of your group.

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.