2


I need to breakRoleInheritance on the items of a SharePoint2013 list.
I need to remove all the current roles and add only the Owner and the Current user.
How can I do it by client side?

Thanks, Nk

1 Answer 1

4

Following example shows how to break role inheritance of a list item and give current user Contribute permissions to the item.

var siteUrl = '/sites/MySiteCollection';

function breakSecurityInheritanceAddUser() {

    var clientContext = new SP.ClientContext(siteUrl);
    var oList = clientContext.get_web().get_lists().getByTitle('MyList');

    var itemId = 4;
    //this.oListItem = oList.get_items().getById(itemId);
    this.oListItem = oList.getItemById(itemId);

    oListItem.breakRoleInheritance(false, false);

    this.oUser = clientContext.get_web().get_currentUser();

    var collRoleDefinitionBinding = SP.RoleDefinitionBindingCollection.newObject(clientContext);

    collRoleDefinitionBinding.add(clientContext.get_web().get_roleDefinitions().getByType(SP.RoleType.contributor));

    oListItem.get_roleAssignments().add(oUser, collRoleDefinitionBinding);  
    clientContext.load(oUser);
    clientContext.load(oListItem);

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

function onQuerySucceeded(sender, args) {

    alert('Role inheritance broken for item ' + 
        this.oListItem.get_item('Title') + 
        ' and new role assignment for ' + 
        this.oUser.get_loginName());
}

function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

This example shows how to give group permissions on the list item:

var siteUrl = '/sites/MySiteCollection';

function breakSecurityInheritanceAddUser() {

    var clientContext = new SP.ClientContext(siteUrl);
    var oList = clientContext.get_web().get_lists().getByTitle('MyList');

    var itemId = 4;
    this.oListItem = oList.get_items().getById(itemId);

    oListItem.breakRoleInheritance(false, false);

    this.oUser = clientContext.get_web().get_currentUser();

    var collRoleDefinitionBinding = SP.RoleDefinitionBindingCollection.newObject(clientContext);

    collRoleDefinitionBinding.add(clientContext.get_web().get_roleDefinitions().getByType(SP.RoleType.fullControl));

    var groupCollection = clientContext.get_web().get_siteGroups();
    var group = groupCollection.getByName("Owner Group Name");

    oListItem.get_roleAssignments().add(group, collRoleDefinitionBinding);  
    clientContext.executeQueryAsync(
    Function.createDelegate(this, this.onQuerySucceeded), 
    Function.createDelegate(this, this.onQueryFailed));
}

UPDATE

oListItem.breakRoleInheritance(false, false); will make sure that no roles are inherited from parent list. However, if you want to explicitly remove a role, then use following sample code:

oListItem.breakRoleInheritance(true);

this.oUser = clientContext.get_web().get_siteUsers().getByLoginName('DOMAIN\\alias');

oListItem.get_roleAssignments().getByPrincipal(oUser).deleteObject();

var collRoleDefinitionBinding = SP.RoleDefinitionBindingCollection.newObject(clientContext);

collRoleDefinitionBinding.add(clientContext.get_web().get_roleDefinitions().getByType(SP.RoleType.administrator));

oListItem.get_roleAssignments().add(oUser, collRoleDefinitionBinding);

See this for more info: http://msdn.microsoft.com/en-us/library/office/hh185013(v=office.14).aspx

6
  • Thanks! How can I remove an user or a role?
    – Nk SP
    Commented Apr 15, 2014 at 10:53
  • @NkSP, see my updated answer Commented Apr 15, 2014 at 11:24
  • Which permission do I need to perform this? My users are in a Contribute group. Is there something like 'SPSecurity.RunWithElevatedPrivileges'?
    – Nk SP
    Commented Apr 15, 2014 at 12:04
  • I think it should be done by a user having admin rights. Commented Apr 15, 2014 at 12:07
  • What if I am trying to do this in provider hosted app. What permission should be given to app for this?
    – Garima
    Commented Oct 16, 2014 at 11:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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