Determining if a User Is in a Permission Group with SharePoint’s Web Services

It’s not unusual to want to know if a user is in a certain permission group and change how a page behaves based on whether or not s/he is.  AlexLee over on EndUserSharePoint.com‘s Stump the Panel asked how you might be able to do this using SharePoint’s Web ServicesjQuery Library for SharePoint Web Services (SPServices) to the rescue!

By calling the GetGroupCollectionFromUser operation of the Users and Groups Web Service, you can determine whether your user is in the group.  Here’s the example code from Alex’s post (altered a little bit for simplicity):

$(document).ready(function() {

   $().SPServices({
     operation: "GetGroupCollectionFromUser",
     userLoginName: $().SPServices.SPGetCurrentUser(),
     async: false,
     completefunc: function(xData, Status) {
        if($(xData.responseXML).find("Group[Name='GroupNameHere']").length == 1)
        {
           ... do something ...
        }
      }
   });
});

Remember that this jQuery call will occur on the client side, so it’s not a good method to use if you’re trying to enforce permissions. However, it can allow you to alter the page on the fly based on the user’s group membership.

Similar Posts

79 Comments

  1. HI Marc,

    I can successfully run this code but based on User Role I want to disable or make read only few fields. Can you help me on this. I write below code but it didn’t work.

    //capture keypress on our read only field and return false
    $(‘#idOfInputField’).keypress(function() {
    return false;
    });

    I found the id of field by View Source

    1. Preet:

      It all depends on what you want the behavior to be. Returning false, as you are doing here, won’t have any noticeable effect. What you may want to do is disable the field and then re-enable it using the PreSaveAction so that whatever value is there is still written back to the item.

      M.

  2. Hi,

    Instead of current user login, can I check for a particular user, which group the user belongs to ? I have the user name in “Lastname, Firstname” format.

    Thanks

  3. Hi,

    This is useful but can I check for a particular user which group the user belongs to ? I have the username in a variable in “Lastname, Firstname” format. I tried putting the variable in userLoginName: myVariable, but didnt work :(
    Any help ?

    Thanks

    1. Rajdeep:

      You’ve asked basically the same question on three of my posts today. I’m happy to answer, but please only ask your question in one place. I get enough questions as it is!
      http://sympmarc.com/2010/10/07/jquery-library-for-sharepoint-web-services-spservices-v0-5-7-released/comment-page-1/#comment-3635
      http://sympmarc.com/2010/01/29/determining-if-a-user-is-in-a-permission-group-with-sharepoints-web-services/comment-page-2/#comment-3636

      The answer depends on whether the user you want information about is the current user or a different user.

      • If it’s the current user, you can use the $().SPServices.SPGetCurrentUser function in SPServices to get the user’s account to work with.
      • If it’s not the current user, then can you explain more about where you have gotten this user’s name? There are probably other options.

      M.

  4. HI Marc,

    This function doesn’t seem to return the groups where an User is part of Security group or email distribution group, but NOT EXPLICITLY added. Is there a method that would return the same? I am aware of IsUserMemberOfGroup() property of User object returns even if user is not explicitly added, but part of a security group. Is there a method with which i could achieve the saae with jquery?

    Thanks,
    Selvaa

  5. hi marc,
    do u know a good solution to hide something on a page for authorization concern ? js is not a good idea to secure something. there must be something to do with xslt to get the groups of current user.

    1. Anonymous:

      You’re right that running script to filter based on security is not, well, secure. Using scripting in this way is useful for targeting and responding to user behavior, but it isn’t security.

      You can use a DVWP with an AggregateDataSource, one of which is the GetGroupCollectionFromUser Web Service operation.

      M.

  6. The GetGroupCollectionFromUser is working for the current user. I am using this on a Records page to hide elements (var rowToHide = document.getElementById(“PSRow0”); $(rowToHide).css (“display”,”none”); ) if the current user is not a member of a Management Group. The candidate’s Last, First is passed via querystring. I was able to split it in to first.last. The problem is that we have two domains. I can add the ID to the qs.

    How can I lookup the login name based on the Candidate’s ID?

      1. I was able to get it done by manually using GetUserInfo on one domain then the other if the first failed. Was trying to use GetListItems for User Information List for the ID, but could not get it to work. Not sure how to pass ID into SPGetCurrentUser. I am just getting started with using jQuery. Your posts have been very helpful.

        1. DB:

          If you check the docs, you can’t pass the ID into SPGetCurrentUser; it’s not one of the options. That’s not the point of the function; it returns info about the *current* user. However, you could certainly build an analogous function to retreive the same information for any known user. Hmm. maybe I should add that the SPServices!

          M.

  7. Hi Marc,

    Could you advise on a similar sample but in ECMAScript that can recognize the user currently logged on and find out if that user belongs to a specific user group (passed in by a parameter)?

    Thank you in advance!
    -Andres

  8. Thanks a lot Marc. This is the best solution I ever got. You saved a lot of time and effort. You taught me how to use JQuery and webservices in sharepoint

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.