Saturday, December 29, 2012

How to identify if user is in a particular permission group using Web Service

Some days back I was faced with a task of changing the behavior of the list new /Edit form based on the user presence in a particular permission group.

Problem Statement: If the user was present in “Members” group I need to hide some fields and if he was present in the “Owners” Group, I need to show all those fields.

Solution: By calling the GetGroupCollectionFromUser operation of the Users and Groups Web Service, you can determine whether your user is in the group. I got this from Marc post.

The only small issue fixed in that post code was that it worked not on every browser, hence I need to modify it a little to support cross browser. I used responseText instead of responseXML.

  1. First we need to add JavaScript directives to the page, I have provided the CDN link which you can directly use or can download the libraries to use it.
  2. 
    
    
    
    
    
  3. Then include the below script to check the current logged user’s group
  4. 
    


    These scripts can be used on CEWP to achieve the desired result.

3 comments:

  1. SPServices is unnecessary and outdated. The best practice is to use CSOM.

    ReplyDelete
  2. Nice one. SPServices are really powerful. Lot of reusable stuff it has.

    ReplyDelete
  3. Here's a general place to look for using the CSOM and a couple of the main objects to look at using when performing this using the CSOM.

    http://msdn.microsoft.com/en-us/library/ff411936(v=office.14).aspx

    // site context basically SPContext for CSOM
    SP.ClientContext();

    // Gets the web site groups, returns GroupCollection object
    SP.ClientContext().get_web().get_siteGroups();

    // Get the current user
    SP.Web.get_currentUser();

    // Get list of users from a group
    SP.Group.get_users();

    ReplyDelete