below is my script to break inheritance of a list item in SharePoint. I am facing the following issue. I have a form, where users submits request through it. All users are in a group called Site Visitors (having a DL, Site is our Site name) having contribute access to Site and list .We have approvers groups having contribute access and other groups with full control access.
****When a user submits a request, the break inheritance is not working and all users in Visitors list is having access to this item. Our requirement is to view the request to the createdby user and approvers only.
The break inheritance is working if the user is from one of the full control group.***
Can you please let me know what mistake I am doing?
var siteUrl = '/sites/MySiteCollection';
function breakSecurityInheritanceAddUser(listItemID) {
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('MyList');
var itemId = listItemID;
this.oListItem = oList.getItemById(itemId);
oListItem.breakRoleInheritance(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));
var groupCollection = clientcontext.get_web().get_siteGroups();
var Approver1_group = groupCollection.getByName("Approver1");
var Approver2_group = groupCollection.getByName("Approver2")
this.oListItem.get_roleAssignments().add(oUser, collRoleDefinitionBinding);
this.oListItem.get_roleAssignments().add(Approver1_group, collRoleDefinitionBinding);
this.oListItem.get_roleAssignments().add(Approver2_group, collRoleDefinitionBinding);
clientContext.load(this.oUser);
clientContext.load(this.oListItem);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded(sender, args) {
alert('Role inheritance broken for item ');
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}