0

I am using SharePoint server 2016. I want to check if logged in user has specific permission or user lies into specific permission group. So I trying to fetch using EffectiveBasePermissions.

Code:

var context = null;  
    var web = null;
context = new SP.ClientContext.get_current();  
        web = context.get_web();  
var perm = web.get_effectiveBasePermissions();
  alert(perm.has(SP.PermissionKind.approveItems));
  context.load(web); 
        context.executeQueryAsync(onSuccessMethod, onFaiureMethodl);

function onSuccessMethod(sender, args) {  
        var userObject = web.get_currentUser();          
        alert('User name:' + userObject.get_title() + '\n Login Name:' + userObject.get_loginName());     } 

    function onFaiureMethodl(sender, args) {
        alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());  
    }

But I am getting error as:

The property or field 'EffectiveBasePermissions' has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.

Is there any better way to achieve same?

Ganesh Sanap - MVP
  • 44,918
  • 21
  • 30
  • 61
user85213
  • 77
  • 1
  • 9

1 Answers1

1

I Guess, you need to load the web with the EffectiveBasePermissions before using it.

Try using below code:

var web = context.get_web();
context.load(web,'EffectiveBasePermissions');

context.executeQueryAsync(function(){
    // Success 
    if (web.get_effectiveBasePermissions().has(SP.PermissionKind.approveItems)) {
        console.log("user have permissions");
    }
    else {
        console.log("user don't have permissions");
    }
},
function(){
    // Error
    console.log("Error");
});

And if you want to check if the user is in specified SharePoint group or not then you can check below similar question:

Check if user is in a specified group

Ganesh Sanap - MVP
  • 44,918
  • 21
  • 30
  • 61