SharePoint: How do I get all the groups for the current user in Javascript and AngularJS?
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {
$scope.UserGroups = [];
var clientContext = new SP.ClientContext.get_current();
this.collGroup = clientContext.get_web().get_siteGroups();
currentUser = clientContext.get_web().get_currentUser();
clientContext.load(collGroup);
clientContext.load(collGroup, 'Include(Users)');
clientContext.load(currentUser);
clientContext.executeQueryAsync(Function.createDelegate(this, onQuerySucceeded), Function.createDelegate(this, onQueryFailed));
function onQuerySucceeded() {
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();
if (oUser.get_loginName() == currentUser.get_loginName())
{
$scope.UserGroups.push({
"ID": oGroup.get_id(),
"Name": oGroup.get_title()
});
}
}
}
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
});
$scope.UserGroups = [];
var clientContext = new SP.ClientContext.get_current();
this.collGroup = clientContext.get_web().get_siteGroups();
currentUser = clientContext.get_web().get_currentUser();
clientContext.load(collGroup);
clientContext.load(collGroup, 'Include(Users)');
clientContext.load(currentUser);
clientContext.executeQueryAsync(Function.createDelegate(this, onQuerySucceeded), Function.createDelegate(this, onQueryFailed));
function onQuerySucceeded() {
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();
if (oUser.get_loginName() == currentUser.get_loginName())
{
$scope.UserGroups.push({
"ID": oGroup.get_id(),
"Name": oGroup.get_title()
});
}
}
}
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
});
Comments
Post a Comment