Thursday, March 7, 2013

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. ?
    1
    2
    3
    4
    5
    <!-- Reference jQuery on the Google CDN -->
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
     
    <!-- Reference SPServices on cdnjs (Cloudflare) -->
  3. Then include the below script to check the current logged user’s group
  4. ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    <script type="text/javascript">
    $(document).ready(function() {
     $().SPServices({
      operation: "GetGroupCollectionFromUser",
      userLoginName: $().SPServices.SPGetCurrentUser(),
      async: false,
      completefunc: function(xData, Status) {
       if($(xData.responseText).find("CAB Owners").length >= 0)
       {
        //alert("Doing the stuff we want");
       }
      }
     });
    });
    </script>


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

No comments:

Post a Comment