Retrieve all users from all groups using jsom in SharePoint

In this jsom example tutorial, I am going to explain to you how to retrieve users from a SharePoint group using jsom in SharePoint Online. Also, I will explain, how to retrieve all users from all groups in SharePoint online using jsom (JavaScript Object Model). Overall I will explain to you about working with users and groups in SharePoint using jsom.

Retrieve users from a SharePoint group using jsom:

First, we want to display how we can retrieve users from a SharePoint group using jsom (JavaScript object model) in SharePoint Online. Here I have a SharePoint group whose ID is 23. I want to retrieve all the users from that SharePoint group.

Here I have taken a button and on that button click, I am displaying all the users from the SharePoint group. The code you can post inside a script editor web part in a web part page in SharePoint Online.

<input type='button' value='Get Users from Group 23' onclick="retrieveAllUsersInGroup();"/>
<br />
<p id="demo"></p>
<script language="javascript" type="text/javascript">
var collUser;
function retrieveAllUsersInGroup() {
var clientContext = new SP.ClientContext("https://onlysharepoint2013.sharepoint.com/sites/Rohit/");
var collGroup = clientContext.get_web().get_siteGroups();
var oGroup = collGroup.getById(6);
collUser = oGroup.get_users();
clientContext.load(collUser);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded() {
var userInfo = '';
var userEnumerator = collUser.getEnumerator();
while (userEnumerator.moveNext()) {
var oUser = userEnumerator.get_current();
userInfo += '<br>User: ' + oUser.get_title() +
'<br>ID: ' + oUser.get_id() +
'<br>Email: ' + oUser.get_email() +
'<br>Login Name: ' + oUser.get_loginName();
}
document.getElementById("demo").innerHTML = userInfo;
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>

Once you can run the above code you can see the output like below:

Retrieve all users from all groups using jsom in SharePoint Online
Retrieve all users from all groups using jsom in SharePoint Online

Retrieve all users from all groups using jsom in SharePoint Online

Now we will see how to retrieve all users from all groups using jsom (javascript object model) in SharePoint Online or SharePoint 2016/2013.

<input type='button' value='Get Users from All Groups' onclick="retrieveAllUsersAllGroups();"/>
<br />
<p id="demo"></p>
<script language="javascript" type="text/javascript">
var collGroup;
function retrieveAllUsersAllGroups() {
var clientContext = new SP.ClientContext("https://onlysharepoint2013.sharepoint.com/sites/Rohit/");
collGroup = clientContext.get_web().get_siteGroups();
clientContext.load(collGroup);
clientContext.load(collGroup, 'Include(Users)');
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded() {
var userInfo = '';
var groupEnumerator = collGroup.getEnumerator();
while (groupEnumerator.moveNext()) {
var oGroup = groupEnumerator.get_current();
userInfo += '<br> Group ID: ' + oGroup.get_id() +
'<br> Group Title: ' + oGroup.get_title();
var collUser = oGroup.get_users();
var userEnumerator = collUser.getEnumerator();
while (userEnumerator.moveNext()) {
var oUser = userEnumerator.get_current();
userInfo +='<br> User: ' + oUser.get_title() +
'<br> Login Name: ' + oUser.get_loginName();
}
}
document.getElementById("demo").innerHTML = userInfo;
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>

Once you run the code you can see the output will display like below:

Retrieve all users from all groups using jsom in SharePoint Online
Retrieve all users from all groups using jsom in SharePoint Online

Retrieve all users from all groups using jsom in SharePoint

Now, we will see how to retrieve all users from all SharePoint groups using JavaScript object model (jsom) in SharePoint Online or SharePoint 2013/2016.

<input type='button' value='Get Users from All Groups' onclick="retrieveAllUsersAllGroups();"/>
<br />
<p id="demo"></p>
<script language="javascript" type="text/javascript">
var collGroup;
function retrieveAllUsersAllGroups() {
var clientContext = new SP.ClientContext("https://onlysharepoint2013.sharepoint.com/sites/Rohit/");
collGroup = clientContext.get_web().get_siteGroups();
clientContext.load(collGroup);
clientContext.load(collGroup, 'Include(Users)');
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded() {
var userInfo = '';
var groupEnumerator = collGroup.getEnumerator();
while (groupEnumerator.moveNext()) {
var oGroup = groupEnumerator.get_current();
var collUser = oGroup.get_users();
var userEnumerator = collUser.getEnumerator();
while (userEnumerator.moveNext()) {
var oUser = userEnumerator.get_current();
userInfo += '<br> Group ID: ' + oGroup.get_id() +
'<br> Group Title: ' + oGroup.get_title() +
'<br> User: ' + oUser.get_title() +
'<br> Login Name: ' + oUser.get_loginName();
}
}
document.getElementById("demo").innerHTML = userInfo;
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>

Read some related jsom SharePoint tutorials:

I hope this article helps you to retrieve all users from a SharePoint group and also this tutorial explains how to retrieve all users in all groups using jsom in SharePoint Online or SharePoint 2013/2016.

  • I’m trying to run the JSOM code against a SP 2013 installation. Following the load of collGroup and its included users, invocation fo executeQueryAsync produces the error “Value does not fall within the expected range”. I do not have access to SharePoint Administrator but know someone who does. Can this be overcome?

  • >