9

I have a requirement where i need to check the access permission of a user against a List or Library only using REST api from my remote salesforce app.

Say for example, I have to send the list name and user name, and get the result as Read, Write, Contribute(Manage), None. I need to display what permission does that user have for that List.

How do I achieve this. Please help me. Thanks in advance.

2
  • Hi, I fave same issue now Did you resolve this problem ? Commented Oct 15, 2018 at 14:26
  • how do you init from json? where does it get values from? do i need to hardcode a list of all permissions ? there is no better way ?
    – Jackson
    Commented Sep 5 at 8:01

4 Answers 4

16

Use EffectiveBasePermissions to get permissions of the user on a list. Example:

http://aissp2013/sites/Team/_api/web/lists/getbytitle('L2')/EffectiveBasePermissions

Note that this will give the permissions of the logged in user. However, if the need is to check for other users permission while being logged in using as a different user then as suggested by @Amit, use getusereffectivepermissions. In any case the end result is high and low permission masks which don't make much sense and a way to decipher what permissions a user has is to use SP.BasePermissions().has as can be seen in below example:

function checkPermissions() {
    var call = jQuery.ajax({
        url: _spPageContextInfo.webAbsoluteUrl +
            "/_api/Web/effectiveBasePermissions",
        type: "GET",
        dataType: "json",
        headers: {
            Accept: "application/json;odata=verbose"
        }
    });

    call.done(function (data, textStatus, jqXHR) {
        var manageListsPerms = new SP.BasePermissions();
        manageListsPerms.initPropertiesFromJson(data.d.EffectiveBasePermissions);

        var manageLists = manageListsPerms.has(SP.PermissionKind.manageLists);

        var message = jQuery("#message");
        message.text("Manage Lists: " + manageLists);
    });
}
10

You could consider the following approach to get List permission levels/roles:

  • get the List effective permissions using /_api/web/lists/getbytitle('<list title>')/getusereffectivepermissions endpoint
  • determine user roles using SP.BasePermissions.has(perm) Method

JavaScript example

function getListUserEffectivePermissions(webUrl,listTitle, accountName) 
{
    var endpointUrl = webUrl +  "/_api/web/lists/getbytitle('" + listTitle + "')/getusereffectivepermissions(@u)?@u='" + encodeURIComponent(accountName) + "'";
    return $.getJSON(endpointUrl);
}

function parseBasePermissions(value)
{
    var permissions = new SP.BasePermissions();
    permissions.initPropertiesFromJson(value);
    var permLevels = [];
    for(var permLevelName in SP.PermissionKind.prototype) {
        if (SP.PermissionKind.hasOwnProperty(permLevelName)) {
            var permLevel = SP.PermissionKind.parse(permLevelName);
            if(permissions.has(permLevel)){
                  permLevels.push(permLevelName);
            }
        }     
    }
    return permLevels;   
}

Usage

var webUrl = _spPageContextInfo.webAbsoluteUrl;  
getListUserEffectivePermissions(webUrl,'Documents','i:0#.f|membership|[email protected]')
        .done(function(data){
            var roles = parseBasePermissions(data);
            console.log(roles); 
        });

Result

["emptyMask", "viewListItems", "addListItems", "editListItems", "deleteListItems", "openItems", "viewVersions", "deleteVersions", "managePersonalViews", "viewFormPages", "open", "viewPages", "createSSCSite", "browseDirectories", "browseUserInfo", "addDelPrivateWebParts", "updatePersonalWebParts", "useClientIntegration", "useRemoteAPIs", "createAlerts", "editMyUserInfo"]
7
  • Do you know why I would always get an "emptyMask" result? I've asked a similar question here Commented Jul 29, 2015 at 3:29
  • @vadim Gremyachev I'm trying to convert the parseBasePermissions to PowerShell, is that doable? Commented Jul 22, 2016 at 15:20
  • @Erik Alexander, that should be doable Commented Jul 22, 2016 at 15:41
  • I'm not seeing how to do this: var permissions = new SP.BasePermissions(); permissions.initPropertiesFromJson(value); part. I don't see something that would allow me to supply the GetUserEffectivePermissions result like that. Commented Jul 22, 2016 at 17:34
  • 1
    thanks, that was the case...
    – WhiteHat
    Commented Apr 10, 2020 at 21:11
2

Below is the REST endpoint for getting effective permission

/getusereffectivepermissions(@user)?@user='<user login name>'

Check below url: https://msdn.microsoft.com/en-us/library/office/dn531433(v=office.15).aspx#bk_ListGetUserEffectivePermissions

0

This should do the job you need:

http://yourserver/yoursite/_api/lists/getbytitle('yourlisttitle')/RoleAssignments/GetByPrincipalId(idOfTheUserOrGroup)/RoleDefinitionBindings

Be aware, that there is no such as elevated permissions in the client side APIs, so the user executing the code should have the right to read permissions.

4
  • in your url .../_api/lists/getbytitle('yourlisttitle')/RoleAssignments/GetByPrincipalId(idOfTheUserOrGroup)/... What is "idOfTheUserOrGroup" ??? Commented Jan 22, 2015 at 9:42
  • In my sample the idOfTheUserOrGroup is an integer, the ID property of the User or the Group whose role assignments on the list you would like to query. Be aware that this request queries the roles the user directly assigned to him on the list, and not the effective permissions of the user on the list (that you probably need) as suggested by @Amit.
    – pholpar
    Commented Jan 22, 2015 at 10:51
  • This doesn't work.'idOfTheUserOrGroup' has to be Id of the group. Say for example, i have a group named 'Managers' and now if i pass the id of this group to your REST url, it will work. But what i want is a user who belongs to 'Managers' group and i want to check his permission directly against the List. if is use 'getusereffectivepermissions' it will give me high and low permission bits. Not sure how to proceed with these bits. Commented Jan 27, 2015 at 4:41
  • 'idOfTheUserOrGroup' may be the id of the person as well, as long as the person has the permissions assigned directly and not via a SP or AD group. For the usage of the 'getusereffectivepermissions' see the code sample from @Nadeem Yousuf. There you should create a BasePermissions instance based on the EffectiveBasePermissions in the response and you can check this agains the pre-defined permissions, like SP.PermissionKind.manageLists (but not to roles like Read / Write / Contribute as you wish).
    – pholpar
    Commented Jan 27, 2015 at 7:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.