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

折月煮酒 提交于 2019-12-01 01:09:55

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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!