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?

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

  • Have you tried this? Is it working for you?? – Ganesh Sanap Aug 1 at 7:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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