0

I have created an addin . I want when the default page loads , so i have to use javascript , to assign unique permissions to only two groups :

Leave Request Members Leave Request Managers

Permissions should be assigned in whole add-in. So in all the lists

Can anyone help me how to do this?

0

Break role inheritance:

<script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>  
<script language="javascript" type="text/javascript">  
    $(document).ready(function()  
    {  
        SP.SOD.executeFunc('sp.js', 'SP.ClientContext', breakInheritance);  
    });  

    function breakInheritance()  
    {  
        //Get the client context and list object    
        var clientContext = new SP.ClientContext.get_current();  
        var oList = clientContext.get_web().get_lists().getByTitle('DemoList');  
        //Break inheritence    
        oList.breakRoleInheritance(true, false);  
        //Load the client context and execcute the batch    
        clientContext.load(oList);  
        clientContext.executeQueryAsync(QuerySuccess, QueryFailure);  
    }  

    function QuerySuccess()  
    {  
        console.log("Inheritence has been broken.");  
    }  

    function QueryFailure(sender, args)  
    {  
        console.log('Request failed' + args.get_message());  
    }  
</script>  

Assign permission to Group:

function ChangePermissions(groupMembershipID,permissionLevel) {
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {
        var clientContext = SP.ClientContext.get_current();
        var web = clientContext.get_web();
        //Get Role Definition by name
        var roleDef = web.get_roleDefinitions().getByName(permissionLevel);
        var roleDefBinding = SP.RoleDefinitionBindingCollection.newObject(clientContext);
        // Add the role to the role definiiton binding.
        roleDefBinding.add(roleDef);
        // Get the RoleAssignmentCollection for the web.
        var assignments = web.get_roleAssignments();
        //Get the role assignment for the group using Group  's membership id
        var groupRoleAssignment = assignments.getByPrincipalId(groupMembershipID);
        groupRoleAssignment.importRoleDefinitionBindings(roleDefBinding);
        groupRoleAssignment.update();
        clientContext.executeQueryAsync(function () {
            alert("Permissions changed !");
        },
                function (sender, args) {
                    console.log(args.get_message());
                });
    });
}

Refer: Change Permission of a SharePoint Group using Javascript CSOM

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.