Get a group of current users using the client object model on the client side of JQuery SP 2010

I am trying to get the current name of the SharePoint group to which they belong. I could not find a method / property providing this information. I was only able to get the username of the current user. Is there a property that provides me with this information that I do not see?

+4
source share
2 answers

There is no direct method to return groups for the current user through javascript.

Here is a post to the MSDN discussion group that describes how to get this information back. If you want to know the name of the group to check 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 a group name, unfortunately there is no direct way to do this. But we can get the current user and get a collection of users for one group. You can then check the collection of users from the same group to see if it contains the current user.

  • Get current user: example

  • Get a collection of groups for the current network: an example

  • Get a specific group

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

     var userCollection = visitorsGroup.get_users(); 
  • Check the user's collection to see if it contains the specified user.

For a simple demonstration, you can see the following document .

+4
source

As Vadim Gremyachev pointed out here , you can get the current user var currentUser = currentContext.get_web().get_currentUser() , then get all groups var allGroups = currentWeb.get_siteGroups();

Here you can scroll through the group to see if your user is in the current group. Therefore, if you have a list of groups that you want to check, members, owners, viewers, simply use this method to determine 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'); } }); 
+2
source

All Articles