Search This Blog

Thursday, September 11, 2014

ShowHidePagecontrol based on user permissions in SharePoint using javascript

$(document).ready(function (){   
     getCurrentUserRole();
 });

 function getCurrentUserRole() {
    var userPerm;
    $().SPServices({
        operation: "GetRolesAndPermissionsForCurrentUser",
        async: false,
        completefunc: function (xData, Status) {
            //if( Status == "success") {
            userPerm = $(xData.responseXML).SPFilterNode("Permissions").attr("Value");
            //alert(userPerm);
           
            var nonAdminP = (33554432 & userPerm) == 33554432;
            //alert("nonAdminP == 33554432: " + nonAdminP);
           
            var adminP = userPerm == 9223372036854775807;
            //alert(adminP);
           
            var hideEdit = !(nonAdminP | adminP);
             //alert("hideEdit = " + hideEdit);
            
             if (hideEdit) {
                 $('#addbutton').css('display','none');
                 $('#btnEditProducts').css('display','none');
            }
            else{
                $('.btn_blue').css('display','block');
            }


           
                $(xData.responseXML).SPFilterNode("[nodeName=Role]").each(function () {
                    alert($(this).attr("Name"));
                    if($(this).attr("Name") == "Full Control") {                       
                        // THE USER IS ASSIGNED THE FULL CONTROL ROLE
                    }
                    if($(this).attr("Name") == "Approver") {
                        // THE USER IS ASSIGNED THE APPROVER ROLE
                    }
                });
            //}
        }
    });
    return userPerm;
}

1 comment:

  1. Thank you so much..... I was looking for the 2nd block...you saved my day :)

    ReplyDelete