Created
March 18, 2016 23:29
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
currentUserIsMemberOf("Members of Demo").done(function(result){ | |
alert(result) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Require jQuery. or implement your own Deferred object like this: http://stackoverflow.com/questions/18096715/implement-deferred-object-without-using-jquery