Skip to content

Instantly share code, notes, and snippets.

@vgrem
Last active April 29, 2021 17:11
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save vgrem/8317710 to your computer and use it in GitHub Desktop.
Save vgrem/8317710 to your computer and use it in GitHub Desktop.
Determine if current user is a member of Group via CSOM(JavaScript) in SharePoint 2010/2013
//Usage
function IsCurrentUserWithContributePerms()
{
IsCurrentUserMemberOfGroup("Members", function (isCurrentUserInGroup) {
if(isCurrentUserInGroup)
{
// The current user is in the [Members] group
}
});
}
ExecuteOrDelayUntilScriptLoaded(IsCurrentUserWithContributePerms, 'SP.js');
function IsCurrentUserMemberOfGroup(groupName, OnComplete) {
var context = new SP.ClientContext.get_current();
var currentWeb = context.get_web();
var currentUser = context.get_web().get_currentUser();
context.load(currentUser);
var allGroups = currentWeb.get_siteGroups();
context.load(allGroups);
var group = allGroups.getByName(groupName);
context.load(group);
var groupUsers = group.get_users();
context.load(groupUsers);
context.executeQueryAsync(
function(sender, args) {
var userInGroup = IsUserInGroup(currentUser,group);
OnComplete(userInGroup);
},
function OnFailure(sender, args) {
OnComplete(false);
}
);
function IsUserInGroup(user,group)
{
var groupUsers = group.get_users();
var userInGroup = false;
var groupUserEnumerator = groupUsers.getEnumerator();
while (groupUserEnumerator.moveNext()) {
var groupUser = groupUserEnumerator.get_current();
if (groupUser.get_id() == user.get_id()) {
userInGroup = true;
break;
}
}
return userInGroup;
}
}
@vgrem
Copy link
Author

vgrem commented Jan 8, 2014

Demonstrates how to determine if current user is a member of specific Group via CSOM(JavaScript) in SharePoint 2010/2013

@robertosljunior
Copy link

If anyone is interested. This approach can be used when you want to check if a user is a member of a group using the group name.

[https://gist.github.com/robertosljunior/cc46d4447cf6d6b60c67]

@soerennielsen
Copy link

Should come with a proper warning that it just iterates the group. What about AD groups in SharePoint groups? Likely hard to do but worth mentioning that it is not covered.

@malexandrum
Copy link

malexandrum commented Jul 12, 2016

As @soerennielsen mentioned, this is almost worthless since site administrators / owners can add AD groups to SharePoint Groups.
To be more specific, if the user you need to check membership is member of AD group HR and HR group is added to SharePoint group "Group 1", when you check if the user is actually a member of Group 1, you get false

@johnnycardy
Copy link

johnnycardy commented Feb 24, 2017

The only solution for this problem that I can think of is to create a list item with a 'User' column set to the group name. Then query the list with a CAML query that specifies that the current user should be a member of the group in that column. If it comes back in the result, then they're a member.

This is obviously slow but with some caching, maybe it'd be okay. It's the only thing I can think of. Thoughts?

@VenkateshKadiri66
Copy link

@Johnycardy That's the exact approach we followed. Replying to a 4-year-old thread as the solution you proposed works elegantly.

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