0

My function for checking List Item permissions in Sharepoint doesn't seem to work. It is placed in aspx file on a Sharepoint 2010 site. I'm getting an error message below when trying to run it. Does anyone know how it should be done ? I found plenty of examples for the same function in C# but can't find anything in Javascript. Thanks !

function checkPermissions(){
var ctx = new SP.ClientContext.get_current();
var oBooksList=ctx.get_web().get_lists().getByTitle('test Repository');
var listItem=oBooksList.getItemById(2);


//ctx.load(listItem.get_roleAssignments());
ctx.load(listItem, 'RoleAssignments');
ctx.executeQueryAsync(
    Function.createDelegate(this, this.onSucceededCallback),
    Function.createDelegate(this, this.onFailedCallback));

}
function onSucceededCallback(sender, args){
    alert('it worked');
}

Error message:

SCRIPT5007: Unable to get value of the property 'apply': object is null or undefined 
Gumis
  • 13
  • 1
  • 3
  • Only thing I can see... (unless it is an omission on purpose) is that there is no `onFailedCallback` function in your code.. That would cause something similar as it calls `Function.prototype.apply` or `onFailedCallback.apply` upon completion. – Michael Coxon Apr 08 '15 at 15:08
  • Thanks Michael, it works after adding onFailedCallback function. However I have difficulty with retrieving callback data, so I can't check if returned data is correct. Any ideas anyone ? – Gumis Apr 08 '15 at 15:57
  • If I remember correctly the fail callback only has 1 argument and that is the error.I think error.get_message() has the error message in it – Michael Coxon Apr 08 '15 at 16:01
  • actually it has 2.. https://msdn.microsoft.com/en-us/library/office/hh185007%28v=office.14%29.aspx `function onQueryFailed(sender, args) { alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace()); }` – Michael Coxon Apr 08 '15 at 16:06
  • Callback run succesfully as onSucceededCallback has been called. I had a look at the Object that was returned in args and it didn't have permission information. So it looks like there is something wrong with my initiall query.... – Gumis Apr 08 '15 at 16:11
  • I recommend reading thought the api at https://msdn.microsoft.com/en-us/library/office/ee538253(v=office.14).aspx it has 'enough' useful info to get you on your way... in regards to getting the role assignments you are after `ctx.load(listItem.get_roleAssignments()` https://msdn.microsoft.com/en-us/library/office/ee551126(v=office.14).aspx this will return a collection that you will need to iterate over – Michael Coxon Apr 08 '15 at 16:16

1 Answers1

2

Something like this should help....

UNTESTED

function checkPermissions(){
    var ctx = new SP.ClientContext.get_current();
    var oBooksList = ctx.get_web().get_lists().getByTitle('test Repository');
    var listItem = oBooksList.getItemById(2);
    var roles = listItem.get_roleAssignments();

    ctx.load(roles);
    ctx.executeQueryAsync(
        // success
        function (sender, args){
            var roleInfo = '';
            var roleEnumerator = roles.getEnumerator();
            // enumerate the roles
            while (roleEnumerator.moveNext()) {
                var role = roleEnumerator.get_current();
                var principal = role.get_member();
                // get the principal
                ctx.load(principal);
                ctx.executeQueryAsync(
                    // success
                    function (sender, args){
                        // alert the title
                        alert(principal.get_title());
                    }, 
                    // failure
                    function (sender, args){
                        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace()); 
                    });
            }
        }, 
        // failure
        function (sender, args){
            alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace()); 
        });

}
Michael Coxon
  • 5,311
  • 1
  • 24
  • 51
  • In the `while`-loop there is a small scoping issue, changing `var principal` into `let principal` should solve this. But in any case a good solution. – winner_joiner Dec 05 '18 at 06:01