Dev Notes

Notes on Development with Microsoft Technologies

Get User Roles using jQuery and SPServices

4 Comments

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

Author: Chris Quick

I have been a developer of web based solutions since early 2001 delivering solutions to a wide array of organizations using ASP, ASP.NET and SharePoint. I was introduced to SharePoint in 2003 when the consulting firm I worked for at the time introduced it into the workplace. I began working with MOSS 2007 as soon as Microsoft released the RTM version in November 2006. The platform was implemented at the organization I worked for in 2007 and went live in March of that year. I was tasked with the administration and ongoing development of the platform. I currently work as a SharePoint Architect with Artis Consulting, developing solutions for a wide variety of business problems. The goal of this blog is to share my discoveries developing solutions with SharePoint. I welcome your comments and feedback to any post -- and I welcome suggestions for future topics.

4 thoughts on “Get User Roles using jQuery and SPServices

  1. if we need to assign user to sp group?

  2. 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

  3. 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

    • 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