In this blog, we will see about how to add a read permission for the user and stop the inherit permissions in the list , using JavaScript Object Model. Using this code, we can break the security inheritance of a Website, list, library, or list item through the BreakRoleInheritance method. When we want to grant an access to the folders or the documents or simply for an item to a specific user, we go ahead and break the Inheritance and provide an access to the specified user.

Add JavaScript files given below in to your solution.
  1. <script type="text/javascript" src="../Scripts/jquery-1.9.1.min.js"></script>
  2. <script type="text/ecmascript" src="../_layouts/SP.core.debug.js" />
  3. <script type="text/ecmascript" src="../_layouts/SP.runtime.debug.js" />
  4. <script type="text/ecmascript" src="../_layouts/SP.debug.js" />
The blog given below shows how to break the security of a list by using the breakRoleInheritance(copyRoleAssignments, clearSubscopes) function of the List object.
  1. <script type="text/ecmascript">
  2. function readPermisiontoUSER() {
  3. var Context = new SP.ClientContext.get_current();
  4. var Website = Context.get_web();
  5. var oList = Website.get_lists().getByTitle('CustomList');
  6. var ListItem = oList.get_items().getById('4');
  7. ListItem.breakRoleInheritance(false);
  8. this.User = Context.get_web().get_siteUsers().getByLoginName('SPDEV\\gowtham');
  9. var collRoleDefinitionBinding = SP.RoleDefinitionBindingCollection.newObject(Context);
  10. collRoleDefinitionBinding.add(Context.get_web().get_roleDefinitions().getByType(SP.RoleType.reader));
  11. oListItem.get_roleAssignments().add(User, collRoleDefinitionBinding);
  12. Context.load(User);
  13. Context.load(ListItem);
  14. Context.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
  15. }
  16. function onQuerySucceeded(sender, args) {
  17. alert('Role inheritance broken for item ');
  18. }
  19. function onQueryFailed(sender, args) {
  20. alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
  21. }
  22. </script>
Thanks for reading my blog.