1
votes

I'm trying to add permissions to specific folders within a document library using the SharePoint 2013 Client Object Model in C#. In effect I'm trying to reproduce the behaviour you get when you "Share" a folder via the UI. This is the code I've got so far, but its not giving the behaviour I'm after. In the code I'm trying to add a single user to the RoleAssigments collection of the folder. Note: The document library does not inherit permissions from the site level.

using (ClientContext ctx = new ClientContext(SPSiteURL))
{
    ctx.AuthenticationMode = ClientAuthenticationMode.Default;
    Web web = ctx.Web;

    Folder AccountFolder = web.GetFolderByServerRelativeUrl("account/" + OurFolderName);
    ctx.Load(AccountFolder);
    ctx.ExecuteQuery();

    ListItem AllFields = AccountFolder.ListItemAllFields;
    ctx.Load(AllFields);
    ctx.ExecuteQuery();

    // Add the user to SharePoint, if they have not already been added
    Principal AccountUser = ctx.Web.EnsureUser(UsersName);
    ctx.Load(AccountUser);
    ctx.ExecuteQuery();

    var info = Utility.ResolvePrincipal(ctx, ctx.Web, AccountUser.LoginName, PrincipalType.All, PrincipalSource.All, null, false);
    context.ExecuteQuery();
    Principal ResolvedUser = context.Web.EnsureUser(info.Value.LoginName);
    ctx.Load(ResolvedUser);
    ctx.ExecuteQuery();

    // Get the existing RoleAssignments collection for the folder
    RoleAssignmentCollection RoleAssignments = AllFields.RoleAssignments;

    // Create a new RoleDefinitionBindingCollection object
    RoleDefinitionBindingCollection collRDB = new RoleDefinitionBindingCollection(ctx);

    // Get the default "Contribute" role and add it to our RoleDefinitionBindingCollection
    RoleDefinition ContributeRoleDef = ctx.Web.RoleDefinitions.GetByName("Contribute");
    collRDB.Add(ContributeRoleDef);

    // Break the Role Inheritance, but copy the parent roles and propagate our roles down
    AllFields.BreakRoleInheritance(true, true);

    // Add our new RoleAssigment to the RoleAssignmentCollection for the folder
    RoleAssignments.Add(ResolvedUser, collRDB);

    //  Push our permission update back to SharePoint
    ctx.ExecuteQuery();
}
1

1 Answers

2
votes

The following example demonstrates how to share folder using CSOM API:

using (var ctx = new ClientContext(webUri))          
{

    var folder = ctx.Web.GetFolderByServerRelativeUrl("/Shared Documents/Archive");
    var folderItem = folder.ListItemAllFields;

    //grant Read permissions to 'Everyone' Sec Group  
    var everyoneSecGroup = ctx.Web.SiteUsers.GetById(4);     //get Everyone security group            
    ShareListItem(folderItem, everyoneSecGroup, "Read");
}

where

public static void ShareListItem(ListItem listItem, Principal principal, string permissionLevelName)
{
     var ctx = listItem.Context as ClientContext;
     var roleDefinition = ctx.Site.RootWeb.RoleDefinitions.GetByName(permissionLevelName);
     listItem.BreakRoleInheritance(true, false);
     var roleBindings = new RoleDefinitionBindingCollection(ctx) { roleDefinition };
     listItem.RoleAssignments.Add(principal, roleBindings);
     ctx.ExecuteQuery();
}

Result

enter image description here