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();  

Anyway 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

2 Answers 2

2

Create a new RoleDefinitionBinding, object set the type of permissions to it and add it as parameter to RoleAssignments of list item:

    using System;
    using Microsoft.SharePoint.Client;
    using SP = Microsoft.SharePoint.Client;

    namespace Microsoft.SDK.SharePointServices.Samples
    {
        class BreakSecurityInheritanceAddUser
        {
            static void Main()
            {
                string siteUrl = "Your site url";
                ClientContext clientContext = new ClientContext(siteUrl);

                var list = clientContext.Web.Lists.GetByTitle("JZhu");
                var items = list.GetItems(CamlQuery.CreateAllItemsQuery());
                clientContext.Load(items);
                clientContext.ExecuteQuery();

                clientContext.Load(clientContext.Web.SiteGroups);
                clientContext.ExecuteQuery();

                GroupCollection oSiteCollectionGroups= clientContext.Web.SiteGroups;
                foreach (Group grp in oSiteCollectionGroups)
                {
                    Console.WriteLine(grp.Title);
                    if (grp.Title == "My group")
                    {
                        oGroup=gpr;
                        break;
                    }
                }

                foreach (var item in items)
                {
                    var folder = GetListItemFolder(item); //get Folder
                    Console.WriteLine(folder.Name);
                    if (folder.Name=="Folder 1" || folder.Name=="Folder 2")
                    {
                        item.BreakRoleInheritance(false);
                        RoleDefinitionBindingCollection collRoleDefinitionBinding = new RoleDefinitionBindingCollection(clientContext);

                        //set role type
                        collRoleDefinitionBinding.Add(clientContext.Web.RoleDefinitions.GetByType(RoleType.Reader));
                        //oGroup - your group
                        item.RoleAssignments.Add(oGroup, collRoleDefinitionBinding);

                        clientContext.ExecuteQuery();
                    }
                }
            }

            private static Folder GetListItemFolder(ListItem listItem)
            {
                var folderUrl = (string)listItem["FileDirRef"];
                var parentFolder = listItem.ParentList.ParentWeb.GetFolderByServerRelativeUrl(folderUrl);
                listItem.Context.Load(parentFolder);
                listItem.Context.ExecuteQuery();
                return parentFolder;
            }
        }
    }
8
  • I do not have a list in my question, what should I put in clientContext.Web.Lists.GetByTitle("Your List Name");?
    – OPK
    Commented Apr 20, 2015 at 18:20
  • where are your folders? They have to be inside document library
    – Nk SP
    Commented Apr 20, 2015 at 18:27
  • I have added a screen shot. Have a look. The folders are inside library called JZhu
    – OPK
    Commented Apr 20, 2015 at 18:29
  • @HashMap updated my answer
    – Nk SP
    Commented Apr 20, 2015 at 18:38
  • @line item.RoleAssignments.Add(oGroup, collRoleDefinitionBinding);, what is oGroup? My program currently does not complie and has red line under oGroup
    – OPK
    Commented Apr 20, 2015 at 18:54
0

Create a new RoleDefinitionBinding, object set the type of permissions to it and add it as parameter to RoleAssignments of list item:

Code Sample:

using (clientContext = new ClientContext(sharepont URL))
            {
                try
                {
                    var securePassword = new SecureString();
                    foreach (char c in Password)
                    {
                        securePassword.AppendChar(c);
                    }
            
                    clientContext.Credentials = new SharePointOnlineCredentials(UserName, securePassword);
                    WebClient = clientContext.Web;

                    List list = clientContext.Web.Lists.GetByTitle("Documents");
                    var rootfolder = list.RootFolder;// find root folder 
                    clientContext.Load(rootfolder);
                    clientContext.ExecuteQuery();
                    string fpath=rootfolder.ServerRelativeUrl+ "Your folder path using "/"; //eg /folderName/A1/A1

                    Principal user = clientContext.Web.EnsureUser(UserName);// UserName : emailid or user name
                    var folder = clientContext.Web.GetFolderByServerRelativeUrl(fpath);
                    var roleDefinition = clientContext.Site.RootWeb.RoleDefinitions.GetByType(RoleType.Editor);  //get Reader role
                    var roleBindings = new RoleDefinitionBindingCollection(clientContext) { roleDefinition };
                    folder.ListItemAllFields.BreakRoleInheritance(true, false);  //set folder unique permissions
                    folder.ListItemAllFields.RoleAssignments.Add(user, roleBindings);
                    clientContext.ExecuteQuery();
                    Console.WriteLine(fpath + "-  folder grantted");
                }
                catch (Exception ex)
                {
                    throw ex;
                }  
            }   

   


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.