SharePoint CSOM 迁移列表项权限


  前言

  最近,在帮客户做数据迁移的项目,艾玛,这真是一言难尽啊,xxx...aafdsjlsadjfljiojoij...

  正文

  好了,吐槽完了,我们说正事儿!其实,迁移权限也没什么复杂的,因为时间比较紧张,自己也没特别看代码的规范,大家凑合看吧,反正我是照镜子不先自己丑,不过也不会对着镜子问谁是最美的。

  代码,真的没什么好说的,整体逻辑就是获取源数据项目,看权限是不是断开,如果断开就迁移权限。

  然后,获取当前项(迁移过程已经创建好),断开权限,清空权限,迁移权限。迁移过程要注意用户和用户组迁移方式不一样,web.ensureuser可能会返回空(大厂小厂离职都是常有的,即使不离职觉得自己账号看腻了,也有改动的),要有异常处理。

  最后,日志要记录清楚,哪个用户迁移的什么权限,或者有迷失的用户或者组什么的。

  好了,最后的最后,大家自己看代码吧~

ListItem currentItem = currentLibrary.GetItemById(currentItemId);
currentContext.Load(currentItem, a => a.HasUniqueRoleAssignments);
currentContext.ExecuteQuery();

if (currentItem.HasUniqueRoleAssignments)
{
    ListItem sourceItem = sourceLibrary.GetItemById(sourceItemId);

    //读取权限
    sourceContext.Load(sourceItem, a => a.RoleAssignments.Include(roleAsg => roleAsg.Member.LoginName, roleAsg => roleAsg.Member.Title,
        roleAsg => roleAsg.RoleDefinitionBindings.Include(roleDef => roleDef.Name,
        roleDef => roleDef.Description, roleDef => roleDef.RoleTypeKind)));
    sourceContext.ExecuteQuery();

    currentContext.Load(currentItem, a => a.RoleAssignments.Include(roleAsg => roleAsg.Member.LoginName, roleAsg => roleAsg.Member.Id, 
        roleAsg => roleAsg.RoleDefinitionBindings.Include(roleDef => roleDef.Name, 
        roleDef => roleDef.Description, roleDef => roleDef.RoleTypeKind)));
    currentContext.ExecuteQuery();

    //清空权限
    for (var m = 0; m < currentItem.RoleAssignments.Count; m++)
    {
        RoleAssignment r = currentItem.RoleAssignments[m];
        currentItem.RoleAssignments.GetByPrincipalId(r.Member.Id).DeleteObject();
        currentContext.ExecuteQuery();
    }

    //设置权限
    for (int j = 0; j < sourceItem.RoleAssignments.Count; j++)
    {
        RoleAssignment roleAsg = sourceItem.RoleAssignments[j];
        var addRole = new RoleDefinitionBindingCollection(currentContext);

        List<string> roles = new List<string>();
        foreach (var role in roleAsg.RoleDefinitionBindings)
        {
            RoleDefinition myRole = currentWeb.RoleDefinitions.GetByName(role.Name);
            addRole.Add(myRole);
            roles.Add(role.Name);
        }

        if (roleAsg.Member.ToString() == "Microsoft.SharePoint.Client.User")
        {
            string userName = roleAsg.Member.LoginName;
            try
            {
                User user = currentWeb.EnsureUser(userName);
                currentItem.RoleAssignments.Add(user, addRole);
                currentItem.Update();
                currentContext.ExecuteQuery();
            }
            catch{}
        }
        else
        {
            string groupName = string.Empty;
            try
            {
                Group group = currentWeb.SiteGroups.GetByName(groupName);
                currentItem.RoleAssignments.Add(group, addRole);
                currentItem.Update();
                currentContext.ExecuteQuery();
            }
            catch{}
        }
    }
}
else
{
    Helper.writeLogs("There is no unique permission.", MigrationLog);
}