SharePoint/PnP-JS-Core

How to: set unique permissions for a folder or file (and so on)

Closed this issue · 4 comments

Category

[ ] Enhancement

[ ] Bug

[x] Question

Version

Please specify what version of the library you are using: [ @pnp/sp - 1.0.4]

Question

How to: set unique permissions to a folder or file (and so on) for group with pnp-js-core (@pnp/sp)?

@bva999,

It's another repository. It's better addressing issues/questions in PnP JS (@pnp scoped) repository.

In regard to assigning unique permissions, this code snippet might help:

(async () => {

  // List, item, file or folder instance
  const list = sp.web.getList(`/sites/dev-a/Lists/ListA`);

  // Break role inheritence for unique permissions
  await list.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 list.roleAssignments.add(principalId, roleDefId);

  console.log(`Done`);

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

Also, this wiki article might be helpful.

Thank you koltyakov,

But what about Folder? Folder doesn't have method breakRoleInheritance and property roleAssignments….
Or I can get folder as item by ID?

Thanks, yup, forget to mention that for Folders and Files the operations should be executed for corresponding items:

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

await folderItem.breakRoleInheritance(false);
// further actions

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

await fileItem.breakRoleInheritance(false);
// further actions

Thanks, that's what I need!