2

I have the following code which works fine when creating folders:

public void CreateFolders()
{
    _SharePoint.ClientContext _ClientContext = new _SharePoint.ClientContext("https://xx_url_xx/sites/oshirodev/");
    _ClientContext.Credentials = new NetworkCredential("user", "pass", "xxxx_url");

    var _web = _ClientContext._web;
    var _Root = _web.Lists.GetByTitle("Library1");
    var _folder1 = _Root.RootFolder.Folders.Add("Folder1");
    var _subfolder1 = _folder1.Folders.Add("SubFolder1");
    _folder1.Update();
    _subfolder1.Update();

    var _folder2 = _Root.RootFolder.Folders.Add("Folder2");
    var _subfolder2 = _folder2.Folders.Add("SubFolder2");
    _folder1.Update();
    _subfolder1.Update();

    _ClientContext.ExecuteQuery();
}

I understand how to change the permissions of everything within a library for example:

public void ChangeFolderPermissions()
{
    _SharePoint.ClientContext _ClientContext = new _SharePoint.ClientContext("https://xx_url_xx/sites/oshirodev/");
    _ClientContext.Credentials = new NetworkCredential("user", "pass", "oshiro.com");

    _SharePoint.Principal _user = _ClientContext.Web.EnsureUser(@"xx_domain\tom");
    _SharePoint.List _item = _ClientContext.Web.Lists.GetByTitle("Library1");
    var roleDefinition = _ClientContext.Site.RootWeb.RoleDefinitions.GetByType(_SharePoint.RoleType.Reader);  //get Reader role
    var roleBindings = new _SharePoint.RoleDefinitionBindingCollection(_ClientContext) { roleDefinition };
    _item.BreakRoleInheritance(true, false);  //set folder unique permissions
    _item.RoleAssignments.Add(_user, roleBindings);
    _ClientContext.ExecuteQuery();
}

But I don't want to change the permissions for everything, I only want to set the permissions for the new folders I am creating.

My question is, how do I set permissions for these folders so only a specific user has access to them?

1
  • You are breaking inheritance of Library1 and changing the permission if library1. So child folders inside Library1 will also get affected. You need to get object of the folder1 and then change permission
    – Unnie
    May 16, 2017 at 15:03

1 Answer 1

0

If you want to set permission only to newly created folder, then don't break inheritance for library. i have updated your code:

Please note that your folder relative URL will be "/site/Library1/Foldername"

  public void ChangeFolderPermissions()
    {
        _SharePoint.ClientContext _ClientContext = new _SharePoint.ClientContext("https://xx_url_xx/sites/oshirodev/");
        _ClientContext.Credentials = new NetworkCredential("user", "pass", "xx_url.com");

        _SharePoint.Principal _user = _ClientContext.Web.EnsureUser(@"xx_domain\tom");
        var folder = _ClientContext.Web.GetFolderByServerRelativeUrl(folderUrl);   
        var roleDefinition = _ClientContext.Site.RootWeb.RoleDefinitions.GetByType(_SharePoint.RoleType.Reader);  //get Reader role
        var roleBindings = new _SharePoint.RoleDefinitionBindingCollection(_ClientContext) { roleDefinition };
        folder.ListItemAllFields.BreakRoleInheritance(true, false);  //set folder unique permissions
        folder.ListItemAllFields.RoleAssignments.Add(user, roleBindings);
        _ClientContext.ExecuteQuery();
    }

Your Answer

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

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