Skip to content

Instantly share code, notes, and snippets.

@robertosljunior
Created March 18, 2016 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertosljunior/cc46d4447cf6d6b60c67 to your computer and use it in GitHub Desktop.
Save robertosljunior/cc46d4447cf6d6b60c67 to your computer and use it in GitHub Desktop.
SharePoint 2010 - Verify if current user is a member of a Group using group name (CSOM Javascript)
var currentUserIsMemberOf = function(groupName){
var found = false;
var dfd = $.Deferred(function(){
SP.SOD.executeOrDelayUntilScriptLoaded(function(){
context = new SP.ClientContext.get_current();
allGroups = context.get_web().get_siteGroups();
context.load(allGroups);
context.load(allGroups, 'Include(Users)');
context.executeQueryAsync(
function(){
var groupInfo;
var groupsEnumerator = allGroups.getEnumerator();
while (groupsEnumerator.moveNext()) {
var group = groupsEnumerator.get_current();
if(group.get_title() == groupName) {
var usersEnumerator = group.get_users().getEnumerator();
while (usersEnumerator.moveNext()) {
var user = usersEnumerator.get_current();
if(user.get_id() == _spPageContextInfo.userId) {
found = true;
break;
}
}
}
}
dfd.resolve(found);
},
function(){
dfd.reject(args.get_message());
}
);
}, 'sp.js');
});
return dfd.promise();
}
currentUserIsMemberOf("Members of Demo").done(function(result){
alert(result)
});
@robertosljunior
Copy link
Author

Require jQuery. or implement your own Deferred object like this: http://stackoverflow.com/questions/18096715/implement-deferred-object-without-using-jquery

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment