Break And Change List Item Permission Using PnP JS In SPFx

Introduction 

 
In SPFx many times we have a requirement like break permissions for the list, list items and etc for the particular users. for eg., we want to break permission of the list item for XYZ user.
 
so in this article, we will see how to implement break permission of the list items using PnP js so we can use this in SPFx. 
 

Implementation

 
For the testing purpose of our PnP js query, we can use the SP Editor extension for chrome. After installation, you can check it in the developer tool. 
 
So open developer tool(F12) > SharePoint Tab > PnP Js Console > At here you can write pnp queries and using ctrl + D we can test it.
 
SP Editor - Run PnP Query 
 
Now we will implement the logic for break permission as below,
  1. For eg. I want to set "Contribute" permission so we have to get a Role definition Id. you can set it as per your requirement. for more details to refer to this article. 
  2. Then get list by title and break list permission. 
  3. Get list item by id. 
  4. Break list item permission and then set permission. To add permission we require two parameters UserId and Role definition ID. The meaning of this method is to give permission to a specific user.
Setting list item permission to one user 
  1. import { sp } from "@pnp/sp/presets/all";  
  2. (async () => {  
  3.      const { Id: ContributeDefId } = await sp.web.roleDefinitions.getByName('Contribute').get();  
  4.       const list = sp.web.lists.getByTitle("Travel requests");  
  5.       await list.breakRoleInheritance(false);  
  6.         
  7.         let permission = list.items.getById(1);  
  8.         await permission.breakRoleInheritance(false);  
  9.         await permission.roleAssignments.add(19, ContributeDefId);  
  10.   
  11.         console.log("Permission Changed")  
  12.   
  13. })().catch(console.log)  
Setting list item permission to multiple users 
 
Here logic will be the same as above. just have to add one array with required user IDs and then will iterate this array and set the permissions. 
  1. import { sp } from "@pnp/sp/presets/all";  
  2. (async () => {  
  3.       const { Id: ContributeDefId } = await sp.web.roleDefinitions.getByName('Contribute').get();  
  4.       let usersForPermission = [9,17,19];  
  5.       const list = sp.web.lists.getByTitle("Travel requests");  
  6.       await list.breakRoleInheritance(false);  
  7.       for (var i = 0; i < usersForPermission.length; i++) {  
  8.         let permission = list.items.getById(9);  
  9.         await permission.breakRoleInheritance(false);  
  10.         await permission.roleAssignments.add(usersForPermission[i], ContributeDefId);  
  11.       }  
  12.       console.log("Permission Changed")  
  13. })().catch(console.log)  
Output
 
After successfully running the query now we will check the permission. so first select the list item and click on the details panel from the right corner second last icon (above the list title). you can see the panel as below,
 
First Output 
 
Click on manage access and then click on Advanced as below,
 
Second Output 
 
It will open another tab and in this, you can see all the list item level permission as below,
 
Third OP 
 

Summary

 
In this article, we have seen how to break and add permission to list items using PNP js.
 
Hope this helps! If it is helpful to you then share it with others. Give your valuable feedback and suggestions in the comments section below.
 
Sharing is caring!