How to grant user permission to certain folders using Client Object Model?

后端 未结 1 789
鱼传尺愫
鱼传尺愫 2021-01-03 16:14

So far I am able to grant user certain permission with the following code:

    ClientContext context = new ClientContext(\"http://myRealURL\");
    Principal         


        
相关标签:
1条回答
  • 2021-01-03 16:57

    The following example demonstrates how to customize access to folder via CSOM. There are two steps:

    1. assign unique permissions for a Folder since by default folder inherits permissions from the parent object (List)
    2. grant user permissions to a folder

    Example:

    Principal user = ctx.Web.EnsureUser(accountName);
    var folder = ctx.Web.GetFolderByServerRelativeUrl(folderUrl);
    var roleDefinition = ctx.Site.RootWeb.RoleDefinitions.GetByType(RoleType.Reader);  //get Reader role
    var roleBindings = new RoleDefinitionBindingCollection(ctx) { roleDefinition };
    folder.ListItemAllFields.BreakRoleInheritance(true, false);  //set folder unique permissions
    folder.ListItemAllFields.RoleAssignments.Add(user, roleBindings);
    ctx.ExecuteQuery();
    

    , where folderUrl parameter corresponds to server relative url for a folder,

    for example /news/documents/archive for the following structure:

    News (site)
      |
      Documents (library)
         |
         Archive (folder)
    
    0 讨论(0)
提交回复
热议问题