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. Satheesh:

      I’m not totally sure I understand your question, but I’ve done some testing of the library with SharePoint 2010 and all of the Web Service calls I’ve looked at work just fine. I think that the client object model will just give you another method to use.

      M.

  1. HI Marc,
    I was wondering like if i can use this function to hide/Show edit and Delete item link based on User is in Group or not.

    If yes then can u please point me in direction how can i use this ?

    Ron

    1. Sure. You’d just need to assign a variable the value returned from the SPServices function and do an xsl:if or xsl:choose (depending on what your goals are).

      M.

  2. Thanks Marc,
    Is there any sample for this as i do not know much about xsl.?
    How can i call SPServices function to hold value in XSL veriable?

    Thanks
    Ron

    1. Ron:

      I don’t have a sample handy, but you can do something like this:

      <xsl:variable name="MyVariable">
        JavaScript which outputs a value here...
      </xsl:variable>
      

      M.

  3. Thanks Marc.
    This is one way there is also another way to do it using jquery selectors to find element and hide it.

    nice article

    Ron

  4. HI Marc
    I ma getting Error Spservices is NULL not an Object.
    any idea on this ?
    i am referencing jquery.SPServices-0.4.6.min.js file

    Ron

  5. Hi Marc,
    I dont understand this line of the code

    if($(xData.responseXML).find(“Group[Name=’GroupNameHere’]”).length == 1)

    Also, when i put the same code in CEWP, it fires even if the user is not the group name i mentioned?

    Kindly Clarify!

    Thanks,
    Kunal

    1. Kunal:

      That line is the test to see if the Group you specify is in the returned XML. Obviously, you need to replace ‘GroupNameHere’ with the Group you are looking for.

      M.

  6. I ran into an issue with the code “userLoginName: $().SPServices.SPGetCurrentUser()”. In my testing I often log in as a different user. The $().SPServices.SPGetCurrentUser() does not seem to refresh after I login as a different user, i.e., if I intially log in as User1, the function is correct. If I subsequently log in as User2, the function still returns “User1”. I have to restart my browswer in order to force it to recogize that I have changed users. Am I doing something incorrectly, or is there a way to force it to really return the current user? thanks

Leave a Reply to Marc Cancel 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.