Search code examples
javascriptsharepointsharepoint-jsom

JSOM - Sharepoint - How to remove all permissions on an item of a list after breaking the inheritance?


I'm trying to add a script on a sharepoint page to help users to manage permissions.

The script have to break inheritance (no problem) and change the permissions (problem) on some doc set in a library.

To manage permissions, I have to do 2 things after breaking inheritance : remove all existing groups (problem) and add particluary groups (no problem).

The problem is with removing all the existing group. I can't get the lists of groups, whatever the type of autorisations they have, of a docset and remove them.

Here my script :

_spBodyOnLoadFunctionNames.push("doTheStuff");

function doTheStuff()
{


    $('#managePermissionsDocSets').click(function(e){

        e.preventDefault();     
        managePermissions();        
    });
}


function managePermissions()
{

    // The library full of docsets
    var ListDocSets = "LibraryWithDocSets";

    this.context = new SP.ClientContext.get_current();
    this.website = context.get_web();
    this.collListItem = website
    .get_lists()
    .getByTitle(ListDocSets)
    .getItems('');

    context.load(website);
    context.load(collListItem);     

    context.executeQueryAsync(
        Function.createDelegate(this, this.stopInheritance), 
        Function.createDelegate(this, this.onQueryFailed)
        );

}



function stopInheritance(sender, args)
{
    context = new SP.ClientContext.get_current();

    var listItemEnumerator = collListItem.getEnumerator();          

    while (listItemEnumerator.moveNext()) 
    {
        docSetItem = listItemEnumerator.get_current();      


        if(docSetItem.get_item("Title"))
        {
            docSetItem.breakRoleInheritance(true);          


            var docSetItemPermsEnumerator =  docSetItem.get_roleAssignments().getEnumerator();
            while (docSetItemPermsEnumerator.moveNext())
            {
                rAssignmentMember = docSetItemPermsEnumerator.get_current().get_member();
                //console.log(rAssignment.get_member().get_id() );
                context.load(docSetItem);
                context.load(rAssignmentMember, "Title", "Id");
                this.context.executeQueryAsync(
                    Function.createDelegate(this, removeAssignment),
                    Function.createDelegate(this, this.onQueryFailed)               
                    );  
                //docSetItem.get_roleAssignments().remove(rAssignment);
            }

            //context.load(docSetItem);
            this.context.executeQueryAsync(
                Function.createDelegate(this, this.onQuerySucceeded),
                Function.createDelegate(this, this.onQueryFailed)               
                );          
        }
        else
        {
            console.log("No title");
        }
    }
}

function removeAssignment(sender, args)
{
    var context =  new SP.ClientContext.get_current();
    var web = context.get_web();

    console.log(docSetItem);
    console.log(rAssignmentMember.get_id());

    docSetItem.get_roleAssignments().remove(rAssignment.get_id());

    this.context.executeQueryAsync(
        Function.createDelegate(this, this.onQuerySucceeded),
        Function.createDelegate(this, this.onQueryFailed)               
        );  
}



function onQuerySucceeded(sender, args) {

    console.log("Success");
    /*console.log(sender);
    console.log(args);*/
}

function onQueryFailed(sender, args) {
    console.log('Request failed. ' + args.get_message() + 
        '\n' + args.get_stackTrace());
}

I tried a lot of things. In this one, I try to remove assigment to each docset item (the inheritance is already broken because I already run the script).

Thanks for the help


Solution

  • Method breakRoleInheritance has overload which take two arguments, so you can use it like this:

    docSetItem.breakRoleInheritance(true, true);
    

    This will break the permission inheritance and remove all permissions.