I am trying to obtain the current user's SharePoint group name that they belong to. I haven't been able to find a method/property that provides that information. I've only been able to get the current user's username. Is there a property that provides me this information that I am not seeing?
There is no direct method for returning the groups for the current user through javascript.
Here is a post to an MSDN discussion group that describes a work around to return this information. If you want to know the group Name for checking permissions, a workaround is here.
So basically:
context = new SP.ClientContext.get_current();
web = context.get_web();
var value = web.get_effectiveBasePermissions();
If you need the Group Name, unfortunately there is no direct way to do that. But we can get the current user and get user collection for one group. Then you can check the user collection from one group to see whether it contains the current user.
Get current user: example
Get group collection for the current web: example
Get specified group
var groupCollection = clientContext.get_web().get_siteGroups();
// Get the visitors group, assuming its ID is 4.
visitorsGroup = groupCollection.getById(4);
Get users for the group
var userCollection = visitorsGroup.get_users();
Check the user collection to see whether it contains the specified user.
For a simple demo you can see the following document.
As indicated by Vadim Gremyachev here you can get the current user var currentUser = currentContext.get_web().get_currentUser()
then get all the groups var allGroups = currentWeb.get_siteGroups();
From here you can loop through the group to see if your user is in the current group. So if you have a list of groups you want to check, Members, Owners, Viewers, then just use this method to detect if they are in each group.
function IsCurrentUserMemberOfGroup(groupName, OnComplete) {
var currentContext = new SP.ClientContext.get_current();
var currentWeb = currentContext.get_web();
var currentUser = currentContext.get_web().get_currentUser();
currentContext.load(currentUser);
var allGroups = currentWeb.get_siteGroups();
currentContext.load(allGroups);
var group = allGroups.getByName(groupName);
currentContext.load(group);
var groupUsers = group.get_users();
currentContext.load(groupUsers);
currentContext.executeQueryAsync(OnSuccess,OnFailure);
function OnSuccess(sender, args) {
var userInGroup = false;
var groupUserEnumerator = groupUsers.getEnumerator();
while (groupUserEnumerator.moveNext()) {
var groupUser = groupUserEnumerator.get_current();
if (groupUser.get_id() == currentUser.get_id()) {
userInGroup = true;
break;
}
}
OnComplete(userInGroup);
}
function OnFailure(sender, args) {
OnComplete(false);
}
}
// example use
window.IsCurrentUserMemberOfGroup("Members", function (isCurrentUserInGroup){
if(isCurrentUserInGroup){
console.log('yep he is');
} else {
console.log('nope he aint');
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With