1

I am running SharePoint Foundation 2010. I have a claims based site setup to use a trusted token provider. I am passing it a set of claims that determine the user's group. They look similar to this: http://schemas.xmlsoap.org/claims/Group. On my SharePoint end I have created some SharePoint groups and users to these SharePoint groups through the claims groups. The permissions are working great and I can manipulate them however.

The issue is I would like to get these users' SharePoint groups from code (preferably jQuery) I have attempted to use the SPServices but haven't had much luck. For some reason I can only obtain the user's group if they have been directly added (not added through their group) I followed this tutorial http://azzu-sheikh.blogspot.com/2013/05/jquery-spservices-to-get-current-user.html and it works well if the user has been directly added.

Anybody have any ideas?

1 Answer 1

0

In addition to UserGroup.GetGroupCollectionFromUser method from Users and Groups SOAP service, you could retrieve groups of which the specified user is a member via CSOM.

How to retrieve groups of which the specified user is a member via CSOM (JavaScript)

function getUserGroups(loginName, Success,Error) {

   var context = new SP.ClientContext.get_current();
   var web = context.get_web();
   var allGroups = web.get_siteGroups();
   context.load(allGroups,'Include(Title,Users)');

   context.executeQueryAsync(
        function(sender, args) {
            var userGroups = findUserGroups(loginName,allGroups);         
            Success(userGroups);
        },
        Error
   );


    function findUserGroups(loginName,groups)
    {
         var userGroups = []; 
         var groupsEnumerator = groups.getEnumerator();
         while (groupsEnumerator.moveNext()) {
             var group = groupsEnumerator.get_current();
             if(isUserInGroup(loginName,group)){
                 userGroups.push(group);
             }
         }
         return userGroups;
     }

     function isUserInGroup(loginName,group)
     {
         var users = group.get_users();
         var userInGroup = false;
         var usersEnumerator = users.getEnumerator();
         while (usersEnumerator.moveNext()) {
             var user = usersEnumerator.get_current();
             if (user.get_loginName().toLowerCase() == loginName.toLowerCase()) {
                  userInGroup = true;
                  break;
             }
         }
         return userInGroup;
      }

}

Example:

var userLogin = 'domain\\userLogin';
getUserGroups(userLogin,function (groups) {
    console.log('User belongs to the following groups:');
    for(var i = 0; i < groups.length;i++) {
        console.log(groups[i].get_title());
    }    
},
function(sender,args){
    console.log('Error:' + args.get_message());
});
4
  • Thanks for the post. What is interesting is this pulls a different set of groups that what is in the "Check Permissions" in the Site Permissions page. From that page I can see 5 SharePoint groups for a particular user, but only one shows up (a different group) from this code snippet. Any ideas?
    – shenn
    Commented Mar 5, 2014 at 15:45
  • Good question, function getUserGroups returns groups of which the specified user is a member. Check permissions page returns permissions levels given to the user. Commented Mar 5, 2014 at 16:48
  • true, but it does list the groups the particular user is a part of.
    – shenn
    Commented Mar 5, 2014 at 17:04
  • Given through the "___" group.
    – shenn
    Commented Mar 5, 2014 at 17:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.