Break Inheritance And Assign Unique Permissions To List In SharePoint 2016 And Office 365

SharePoint implements permission inheritance at every level. It means that the permission of the parent is inherited by the child element. Let's say, if the user has the permission to access a site then each list within the site can also be accessed by the same user as the list inherits permission from Site Parent. However, it is not possible in every business scenario. There would be the requirement where we have to assign a unique permission to a child object so that only specific users can access the sensitive contents within the child.

We can break the inheritance out of the box from the permissions page by following the steps, listed below:

  • Open the permissions page of the SharePoint object say site/list and break the inheritance.

  • Remove the groups or the users, who don’t have the privilege to view the contents.

  • Grant permissions to the new groups or the users, who have the privilege to view the contents.

    permission

    We can implement this using JavaScript object model, as well. Let’s see how to do it.
JavaScript Object Model Implementation
  • Add reference to jquery file, 
    1. <script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>  
    2. <script language="javascript" type="text/javascript">  
  • Within the document, ready function is to be called- SP.SOD.executeFunc, so as to load the on the demand script SP.js . Call the main starting point function, breakInheritance.
    1. SP.SOD.executeFunc('sp.js''SP.ClientContext', breakInheritance);  
  • Instantiate the client context and get the list instance. Once, the list object is retrieved, break inheritance of the object.
    1. var clientContext = new SP.ClientContext.get_current();  
    2. var oList = clientContext.get_web().get_lists().getByTitle('DemoList');  
    3.   
    4. //Break inheritence  
    5. oList.breakRoleInheritance(truefalse);  
  • Load the client context and execute the batch, which will send the request to the Server and perform the entire JavaScript object model operations as a batch.
    1. clientContext.load(oList);  
    2. clientContext.executeQueryAsync(QuerySuccess,QueryFailure);  
    Output

    Output

Reset Permission Inheritance

Now, we can reset the inheritance, so that the object inherits permission from its parent object using resetRoleInheritance method. as shown below:

  1. var clientContext = new SP.ClientContext.get_current();  
  2. var oList = clientContext.get_web().get_lists().getByTitle('DemoList');  
  3.   
  4. //Reset inheritence  
  5. oList.resetRoleInheritance();  
method

We can test this in SharePoint, by adding it to the Content Editor Web part, as shown below: 
  • Save the code, given below to a text file and save it into one of the SharePoint Library, Site Assets.
    1. <script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>  
    2. <script language="javascript" type="text/javascript">  
    3.     $(document).ready(function()  
    4.     {  
    5.         SP.SOD.executeFunc('sp.js''SP.ClientContext', breakInheritance);  
    6.     });  
    7.   
    8.     function breakInheritance()  
    9.     {  
    10.         //Get the client context and list object    
    11.         var clientContext = new SP.ClientContext.get_current();  
    12.         var oList = clientContext.get_web().get_lists().getByTitle('DemoList');  
    13.         //Break inheritence    
    14.         oList.breakRoleInheritance(truefalse);  
    15.         //Load the client context and execcute the batch    
    16.         clientContext.load(oList);  
    17.         clientContext.executeQueryAsync(QuerySuccess, QueryFailure);  
    18.     }  
    19.   
    20.     function QuerySuccess()  
    21.     {  
    22.         console.log("Inheritence has been broken.");  
    23.     }  
    24.   
    25.     function QueryFailure(sender, args)  
    26.     {  
    27.         console.log('Request failed' + args.get_message());  
    28.     }  
    29. </script>  
  • Go to the edit settings of the SharePoint page and click on Web part from the Insert tab, as shown below:

    edit settings
  • Add Content Editor Web part,

    Content Editor
  • Click on Edit Web art from Content Edit Web part. Assign the URL of the script text file and click Apply.
    Apply
    We can see that the permission has been broken for the list, as shown below:

    permissions
Thus, we have seen how to break the permission inheritance as well as how to restore the inheritance with the help of this article. In order to see how it’s done using REST, refe to this article.