Pnpjs: BreakRoleInheritance on ListItem

Created on 14 Nov 2018  路  6Comments  路  Source: pnp/pnpjs

Category

  • [ ] Enhancement
  • [ ] Bug
  • [X ] Question
  • [ ] Documentation gap/issue

Hello. I want to ask how can I use BreakRoleInheritance on ListItem and set custom permission for specific user.
Thank you very much.

documentation answered question

Most helpful comment

Hi @enti333,

Copying an answer sample from another question ticket.

import { sp } from '@pnp/sp';

(async () => {

  // List, item, file or folder instance
  const list = sp.web.getList(`/sites/site/Lists/List`);
  const item = list.items.getById(3);

  /*
  const folder = sp.web.getFolderByServerRelativePath('Shared%20Documents/some_folder');
  const folderItem = await folder.getItem();

  const file = sp.web.getFileByServerRelativePath('Shared%20Documents/some_file.docx');
  const fileItem = await file.getItem();
  */

  // Break role inheritence for unique permissions
  await item.breakRoleInheritance(false); // Method receives params

  // Get user/group proncipal Id
  const { Id: principalId } = await sp.web.currentUser.select('Id').get();
  // Get role definition Id
  const { Id: roleDefId } = await sp.web.roleDefinitions.getByName('Edit').get();

  // Assigning permissions
  await item.roleAssignments.add(principalId, roleDefId);

  console.log(`Done`);

})().catch(console.log);

Also, this docs article might be helpful.

All 6 comments

Hi @enti333,

Copying an answer sample from another question ticket.

import { sp } from '@pnp/sp';

(async () => {

  // List, item, file or folder instance
  const list = sp.web.getList(`/sites/site/Lists/List`);
  const item = list.items.getById(3);

  /*
  const folder = sp.web.getFolderByServerRelativePath('Shared%20Documents/some_folder');
  const folderItem = await folder.getItem();

  const file = sp.web.getFileByServerRelativePath('Shared%20Documents/some_file.docx');
  const fileItem = await file.getItem();
  */

  // Break role inheritence for unique permissions
  await item.breakRoleInheritance(false); // Method receives params

  // Get user/group proncipal Id
  const { Id: principalId } = await sp.web.currentUser.select('Id').get();
  // Get role definition Id
  const { Id: roleDefId } = await sp.web.roleDefinitions.getByName('Edit').get();

  // Assigning permissions
  await item.roleAssignments.add(principalId, roleDefId);

  console.log(`Done`);

})().catch(console.log);

Also, this docs article might be helpful.

Going to close this as answered. Please _reopen_ if you have additional questions. Thanks!

Thank you very much koltyakov. It is working without any problem....I am sorry I didn't find it in documentation...

Hi koltyakov,
$pnp.sp.web.lists.getByTitle("testlist").items.getById(8).get().then(function(results){
results.breakRoleInheritance(false);
});

After executing this i want to assign permission for this individual item. Need to assign some groups and individual users. Could you please assist me.

Hi koltyakov,

I have a doubt if person has only Edit with contribute permission on the site level. Using this "Edit" permission how user can break the item permission. As of now user who is having "Full Conrol" only can break the item permission. In this scenario user is having only Edit permission but needs to break the item permission.

Could you please assist me.

Thanks

Sure you have to have a proper permissions level.

Was this page helpful?
0 / 5 - 0 ratings