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.
- <script type="text/javascript" src="../Scripts/jquery-1.9.1.min.js"></script>
- <script type="text/ecmascript" src="../_layouts/SP.core.debug.js" />
- <script type="text/ecmascript" src="../_layouts/SP.runtime.debug.js" />
- <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.
- <script type="text/ecmascript">
- function readPermisiontoUSER() {
- var Context = new SP.ClientContext.get_current();
- var Website = Context.get_web();
- var oList = Website.get_lists().getByTitle('CustomList');
-
- var ListItem = oList.get_items().getById('4');
-
- ListItem.breakRoleInheritance(false);
- this.User = Context.get_web().get_siteUsers().getByLoginName('SPDEV\\gowtham');
- var collRoleDefinitionBinding = SP.RoleDefinitionBindingCollection.newObject(Context);
- collRoleDefinitionBinding.add(Context.get_web().get_roleDefinitions().getByType(SP.RoleType.reader));
- oListItem.get_roleAssignments().add(User, collRoleDefinitionBinding);
- Context.load(User);
- Context.load(ListItem);
- Context.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());
- }
- </script>
Thanks for reading my blog.