Get all SharePoint groups using JavaScript Object Model (jsom)

In this SharePoint jsom tutorials, we will discuss, how to retrieve all the SharePoint groups on the site using the JavaScript object model in SharePoint 2013/2016 or SharePoint Online.

If you are new to JavaScript client object model (jsom), then read Working with JavaScript object model (jsom) in SharePoint 2013/2016/Online.

Get all SharePoint groups using jsom

Below is the code to get all SharePoint groups using jsom (Javascript Object Model) which I can put the below code inside a script editor web part in a page in SharePoint.

<input type='button' value='Get All Groups' onclick="clickMethod();"/>

<br />

<script language="javascript" type="text/javascript">

var siteGroups =";
function clickMethod() {

var clientContext = new SP.ClientContext.get_current();

siteGroups = clientContext.get_web().get_siteGroups();
clientContext.load(siteGroups);
clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);
}

function onQuerySucceeded() {

var allGroups='Group Name: Group ID ‘+'\n';
for (var i =0 ; i < siteGroups.get_count(); i++)
{
allGroups +=siteGroups.itemAt(i).get_title()+' ‘+siteGroups.itemAt(i).get_id()+'\n';
}
alert(allGroups);
}
function onQueryFailed() {

alert(‘Request failed.');
}
</script>

When you click on the button, it will display all the SharePoint groups and the group id like below:

get all sharepoint groups using jsom
get all sharepoint groups using jsom

You may like following SharePoint jsom tutorials:

Hope this SharePoint tutorial explains, how to get all sharepoint groups using the JavaScript object model (jsom).

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

  • >