You can find all current user groups in SharePoint using CSOM (JavaScript). Here is the code I used to find groups :
function GetCurrentUserGroups() {
var clientContext = new SP.ClientContext.get_current();
this.currentUser = clientContext.get_web().get_currentUser();
clientContext.load(this.currentUser);
this.userGroups = this.currentUser.get_groups();
clientContext.load(this.userGroups);
clientContext.executeQueryAsync(Function.createDelegate(this, this.OnQuerySucceeded), Function.createDelegate(this, this.OnQueryFailed));
}
function OnQuerySucceeded(sender, args) {
var g1 = "Visiteurs LMRH"; var g2 = "Visiteurs CASA"; var g3 = "Visiteurs CA";
var membership = "LMRH";
var groupsEnumerator = this.userGroups.getEnumerator();
while (groupsEnumerator.moveNext()) {
var group= groupsEnumerator.get_current();
var groupTitle = group.get_title();
//do something
}
}
function OnQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
ExecuteOrDelayUntilScriptLoaded(GetCurrentUserGroups, 'SP.js');
You can also use the Rest API to get current user groups and below the code to do that :
function GetCurrentUserGroups()
{
var requestUrl = _spPageContextInfo.webServerRelativeUrl + '/_api/web/currentuser/?$expand=groups';
return $.ajax({
url: requestUrl,
async: false,
method: "GET",
contentType: "application/json;odata=verbose",
headers: {
"Accept": "application/json;odata=verbose"
}
});
}
GetCurrentUserGroups()
.done(function(data)
{
var userGroups = data.d.Groups.results;
var currentUserGroups = [];
for(i = 0; i < userGroups.length; ++i){
currentUserGroups.push(userGroups[i].LoginName);
}
})
.fail(
function(error){
console.log(JSON.stringify(error));
});