Search code examples
sharepointcsomsharepoint-apisharepoint-clientobjectspservices

Read All users related to RoleDefinition of ProjectSite using CSOM


I need to loop through all RoleDefinitiona of ProjectSite and get all users related to each one and add those users to a RoleDefinition of a ProjectSite in another SiteCollection

I can loop through RoleDefinitions like :

using (var src_ctx = new ClientContext(Root))
        {
            RoleDefinitionCollection role_definition_collection = src_ctx.Web.RoleDefinitions;
            src_ctx.Load(role_definition_collection);
            src_ctx.ExecuteQuery();
            foreach (RoleDefinition role_definition in role_definition_collection)
            {                            
            }
        }

also I can loop through all groups and read users of each group then get RoleAssignment of each user:

 GroupCollection group_collection = src_ctx.Web.SiteGroups;
            src_ctx.Load(group_collection);
            src_ctx.ExecuteQuery();
            foreach (Group group in group_collection)
            {
                UserCollection user_collection = group.Users;
                foreach (User user in user_collection)
                {
                    RoleAssignment role_assignment = src_ctx.Web.RoleAssignments.GetByPrincipal(user);
                    RoleDefinitionBindingCollection role_definition_binding_collection = role_assignment.RoleDefinitionBindings;
                }
            }

but how can I link between RoleDefinition and users ?


Solution

  • the following class used to copy users with its permissions, may help someone

    public static class UserHelper
    {
        // static members
        private static Dictionary<string, string> Levels = new Dictionary<string, string>();
    
        private static void Prepare_Dictionary()
        {
            Levels["Full Control"] = "Full Control";
            Levels["Design"] = "Design";
            Levels["Contribute"] = "Contribute";
            Levels["Read"] = "Read";
            Levels["Limited Access"] = "Limited Access";
        }
    
        public static void Start_Moving_Users(string Root2010,string Root2013)
        {
            Prepare_Dictionary();
            using (ClientContext src_ctx = new ClientContext(Root2010))
            {
                using (ClientContext dest_ctx = new ClientContext(Root2013))
                {
                    dest_ctx.Web.BreakRoleInheritance(true, false);
                    RoleAssignmentCollection src_rac = src_ctx.Web.RoleAssignments;
                    src_ctx.Load(src_rac);
                    src_ctx.ExecuteQuery();
    
                    foreach (RoleAssignment src_ra in src_rac)
                    {
                        try
                        {
                            src_ctx.Load(src_ra.Member);
                            src_ctx.ExecuteQuery();
                            RoleDefinitionBindingCollection src_rdb = src_ra.RoleDefinitionBindings;
                            src_ctx.Load(src_rdb);
                            src_ctx.ExecuteQuery();
                            RoleDefinition src_rd = src_rdb[0];
                            src_ctx.Load(src_rd);
                            src_ctx.ExecuteQuery();
    
                            Principal dest_user = dest_ctx.Web.EnsureUser(src_ra.Member.LoginName);
                            RoleDefinition dest_rd = dest_ctx.Web.RoleDefinitions.GetByName(Levels[src_rd.Name]);
                            RoleDefinitionBindingCollection dest_rdb = new RoleDefinitionBindingCollection(dest_ctx);
                            dest_rdb.Add(dest_rd);
                            dest_ctx.Web.RoleAssignments.Add(dest_user, dest_rdb);
                            dest_ctx.ExecuteQuery();
                        }
                        catch { continue; }
                    }
                }
            }
        }
    }