SharePoint / PnP-JS-Core

Code moved to https://github.com/pnp/pnpjs. This repository is archived.
Other
379 stars 231 forks source link

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

Closed bva999 closed 6 years ago

bva999 commented 6 years ago

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)?

koltyakov commented 6 years ago

@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.

bva999 commented 6 years ago

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?

koltyakov commented 6 years ago

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
bva999 commented 6 years ago

Thanks, that's what I need!