1

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

    ClientContext context = new ClientContext("http://myRealURL");
    Principal user = context.Web.EnsureUser(@"myLoginAccout");

    RoleDefinition readDef = context.Web.RoleDefinitions.GetByName("Read");
    RoleDefinitionBindingCollection roleDefCollection = new RoleDefinitionBindingCollection(context);
    roleDefCollection.Add(readDef);
    RoleAssignment newRoleAssignment = context.Web.RoleAssignments.Add(user, roleDefCollection);

    context.ExecuteQuery(); 

The code above works fine, now my task is to add the user permission only to certain folders with C# code. For example, under Libraries, I have a library called JZhu, and inside JZhu, I have two folders folder1 and folder2. Is it possible to change the access permission on these two folders with Client Object Model?

enter image description here

enter image description here

OPK
  • 4,120
  • 6
  • 36
  • 66

1 Answers1

4

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)
Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193
  • Ok, I am having trouble to put the correct `folderUrl`. I have attached a screen shot of my folder structure, and my `folderUrl` is like this `"Test/JZhu/folder1"`. Why is getting exception saying `fileNotFound`? – OPK Apr 21 '15 at 12:28
  • Just try to append / in the beginning since it is server relative url, for example: /Test/JZhu/folder1 – Vadim Gremyachev Apr 21 '15 at 12:41
  • That seems solved the problem, but I am getting another exception. Right now it says `value do not fall within rage`. Again I uploaded a screen shot, please advise. – OPK Apr 21 '15 at 12:49
  • Hm, this error occurs mainly when specified object could not be found at the specified url (folderurl in your case). Please make sure the url is valid, for example 1) documents library url is "Documents" 2) folderurl should contain root web url, for example: /rootweb/subweb/documents/folder/subfolder – Vadim Gremyachev Apr 21 '15 at 12:55
  • @VadimGremyachev This answer doesn't make sense as OP tagged this question as sharepoint 2010, and `ListItemAllFields` was not introduced until sharepoint 2013. – oshirowanen May 18 '17 at 09:56