Wednesday, March 17, 2021

How to check whether the current user has edit permission for a particular list using JSOM in SharePoint 2013

 we first need to get the login name of user and pass it to the get_effectiveBasePermissions method.


To ensure that user has edit permission, we will check the SP.PermissionKind.editListItems enum.


SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {

    getCurrentUserPermission();

});



function getCurrentUserPermission()

{    

    var web,clientContext,currentUser,oList,perMask;


    clientContext = new SP.ClientContext.get_current();

    web = clientContext.get_web();

    currentUser = web.get_currentUser();   

    oList = web.get_lists().getByTitle('Test List');

    clientContext.load(oList,'EffectiveBasePermissions');

    clientContext.load(currentUser); 

    clientContext.load(web);           


    clientContext.executeQueryAsync(function(){

        if (oList.get_effectiveBasePermissions().has(SP.PermissionKind.editListItems)){

            console.log("user has edit permission");

        }else{

             console.log("user doesn't have edit permission");

        }   

    }, function(sender, args){

        console.log('request failed ' + args.get_message() + '\n'+ args.get_stackTrace());

    });

}

No comments:

Post a Comment