0

Can we perform the following actions using PnP Core SDK @ https://pnp.github.io/pnpcore/?

  1. Access the permission of a folder

enter image description here

enter image description here

  1. Stop Permission inheritance

enter image description here

  1. Set unique permission

enter image description here

please i do not want to share the folder using links >> i want to use the exact above steps? are those possible? Thanks

1 Answer 1

0

You can use BreakRoleInheritance method of ListItemAllFields property. In my code snippet "Test Library" is the document library title and "Test Folder" the name of the target subfolder.

using (var scope = host.Services.CreateScope()) 
{
    // Ask an IPnPContextFactory from the host
    var pnpContextFactory = scope.ServiceProvider.GetRequiredService<IPnPContextFactory>();

    // Create a PnPContext
    using (var context = await pnpContextFactory.CreateAsync(new Uri(siteUrl)))
    {
        // Get Root Folder
        var rootFolder = (await context.Web.Lists.GetByTitleAsync("Test Library", p => p.RootFolder)).RootFolder;

        // Ensure SubFolder
        var subFolder = await rootFolder.EnsureFolderAsync("TestFolder");

        // Break Inheritance on SubFolder [BreakRoleInheritance(bool copyRoleAssignments, bool clearSubscopes)]
        subFolder.ListItemAllFields.BreakRoleInheritance(false, true);

        // Get current user
        var currentUser = await context.Web.GetCurrentUserAsync();

        // Find role definition
        var roleDefinitions = (await context.Web.GetAsync(p => p.RoleDefinitions)).RoleDefinitions;
        var editRole = roleDefinitions.AsRequested().FirstOrDefault(p => p.Name == "Edit");

        // Add role to selected user
        await subFolder.ListItemAllFields.AddRoleDefinitionAsync(currentUser.Id, editRole);
    }
}
1
  • your code worked well for me .. thanks a lot
    – John John
    Feb 18 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.