使用 CSOM 读取与 ProjectSite 的 RoleDefinition 相关的所有用户

Read All users related to RoleDefinition of ProjectSite using CSOM

我需要遍历 ProjectSite 的所有 RoleDefinitiona 并获取与每个用户相关的所有用户,然后将这些用户添加到另一个 SiteCollection 中 ProjectSite 的 RoleDefinition

我可以像这样遍历 RoleDefinitions:

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)
            {                            
            }
        }

我还可以遍历所有组并读取每个组的用户,然后获取每个用户的 RoleAssignment:

 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;
                }
            }

但是我如何在 RoleDefinition 和用户之间 link?

以下class用于复制用户及其权限,可能对某人有帮助

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; }
                }
            }
        }
    }
}