Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Current Users Group using the SP 2010 javascript Client Side Object Model

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?

like image 748
John F. Avatar asked Jun 21 '11 15:06

John F.


2 Answers

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.

  1. Get current user: example

  2. Get group collection for the current web: example

  3. Get specified group

    var groupCollection = clientContext.get_web().get_siteGroups();
    // Get the visitors group, assuming its ID is 4.
    visitorsGroup = groupCollection.getById(4);
    
  4. Get users for the group

    var userCollection = visitorsGroup.get_users();
    
  5. Check the user collection to see whether it contains the specified user.

For a simple demo you can see the following document.

like image 80
Rich Ross Avatar answered Sep 27 '22 17:09

Rich Ross


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');
    }
});
like image 39
svnm Avatar answered Sep 27 '22 17:09

svnm