Friday, March 4, 2016

Setting permission for a library to a user using CSOM

            string siteURL = "<SITEURL>";
            ClientContext context = new ClientContext(siteURL);
            List list = context.Web.Lists.GetByTitle("<LIBRARYNAME>");
            User user = context.Web.EnsureUser(@"<DOMAIN\USERNAME>");
            list.BreakRoleInheritance(true, false);
            RoleDefinition roleDefinition = context.Web.RoleDefinitions.GetByName("<PERMISSION LEVEL>");
            RoleDefinitionBindingCollection collRoleDefinitionBinding = new RoleDefinitionBindingCollection(context);
            collRoleDefinitionBinding.Add(roleDefinition);
            list.RoleAssignments.Add(user, collRoleDefinitionBinding);
            context.ExecuteQuery(); 

No comments:

Post a Comment