March 19, 2024

SamTech 365

PowerPlatform, Power Apps, Power Automate, PVA, SharePoint, C#, .Net, SQL, Azure News, Tips ….etc

Get all SharePoint Groups using SP.JS

In this post, I will show you how you can retrieve all the SharePoint groups in a Site using SP.JS.

I needed to implement this JavaScript code to populate a bootstrap dropdown for a project which required custom development using BootStrap, SP.JS and other client side technologies.

var RootSite = "https://YourTenant.sharepoint.com/sites/YourSite/"

function Get_Groups() {
    var clientContext = new SP.ClientContext(RootSite);
    this.collGroup = clientContext.get_web().get_siteGroups();
    clientContext.load(collGroup);
    clientContext.executeQueryAsync(Function.createDelegate(this, this.DD_GroupsSucceeded), Function.createDelegate(this, this.DD_GroupsFailed));
}

function DD_GroupsSucceeded(sender, args) {
    console.log('DD_GroupsSucceeded');
    var userInfo = '';

    var groupEnumerator = collGroup.getEnumerator();
    while (groupEnumerator.moveNext()) {
        var oGroup = groupEnumerator.get_current();
        var Group_ID=oGroup.get_id();
        var Group_Title=oGroup.get_title();
        alert (Group_ID +' - ' + Group_Title);
    }
}

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