Get User Roles using jQuery and SPServices

If you ever have a need to get the roles a user is assigned using jQuery – here’s how. You will need to get jQuery (www.jquery.com) and SPServices (spservices.codeplex.com). Then you can perform the following:

    function getCurrentUserRole() {
        $().SPServices({
            operation: "GetRolesAndPermissionsForCurrentUser",
            async: false,
            completefunc: function (xData, Status) {
                if( Status == "success") {
                    $(xData.responseXML).find("[nodeName=Role]").each(function () {
                        if($(this).attr("Name") == "Full Control") {
                            // THE USER IS ASSIGNED THE FULL CONTROL ROLE
                        }
                        if($(this).attr("Name") == "Approver") {
                            // THE USER IS ASSIGNED THE APPROVER ROLE
                        }
                    });
                }
            }
        });
    }

You can check for any role that is defined in SharePoint (this includes roles you might create, i.e. Contribute No Delete). Using this, you could provide UI cues or change the UI for certain users. Remember, this does not “secure” a site. It only responds to the roles the user is assigned.

Advertisements

4 thoughts on “Get User Roles using jQuery and SPServices

  1. I am getting ‘error’ msg in Status variable

    function getCurrentUserRole(roleName) {
    alert(‘called Role ‘+roleName); // this comes with roleName
    $().SPServices({
    operation: “GetRolesAndPermissionsForCurrentUser”,
    async: false,
    completefunc: function (xData, Status) {
    if( Status == “success”) {
    $(xData.responseXML).find(“[nodeName=Role]”).each(function () {
    if($(this).attr(“Name”).indexOf(roleName)>=0) {
    alert(‘yess’);return 0;
    }else{ alert(‘noo..’);return 1;}
    });
    }else{alert(Status);} // This alert comes and stops JS execution.
    }
    });
    }
    $(document).ready(function() {
    getCurrentUserRole(“Visitor”);

    });//Doc.ready func

    I have check /_vti_bin/usergroup.asmx on-clicking “GetRolesAndPermissionsForCurrentUser” an Xml gets generated in browser. And in my another script I am able to get subsite of collection and I am CA/SiteCollection Admin What M I missing? or doing incorrecf

  2. HI,
    I have been using this ( a very similar version of) to load contents depending on the current user’s permissions. Works great.

    I am now trying to get the permissions of any user of choice, not just the logged in user.
    Would you know how to do that?
    GetRoleCollectionFromUser always returns “User not found”..

    Thank you,
    vinz

    1. If a user is not directly assigned a permission (as is usually the case, since most users get permissions through groups), the method would return “User Not Found”. This is apparently the correct behavior. If you encounter this, you need to check all the groups the user is in, and determine whether they have the permission.

      Here’s a code snippet:
      (He forgot to change the code comments and it’s supposed to be “permission” where it says “full control”.)
      http://stackoverflow.com/a/1333666/1939734

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s