在SharePoint 2013中使用CSOM根据特定权限获取所有用户 [英] Get all the users based on a specific permission using CSOM in SharePoint 2013

查看:159
本文介绍了在SharePoint 2013中使用CSOM根据特定权限获取所有用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取在站点上具有特定角色/权限的所有用户的列表.例如,我需要使用SharePoint 2013中的 .Net CSOM 具有编辑权限(RoleType = Editor)的用户列表.他们可以在任何组中.我尝试了很多事情.但是,似乎没有直接的方法可以做到这一点.有任何想法吗?

I want to get the list of all the users who have a particular role/permission on a site. For example, I need the list of users who have edit rights (RoleType=Editor) using .Net CSOM in SharePoint 2013. They can be in any group. I tried many things. However, it seems there isn't a straight forward way to do this. Any ideas?

非常感谢.

推荐答案

您可以利用

You could utilize Web.GetUserEffectivePermissions method to gets the effective permissions that the specified user has within the web site.

示例1:通过权限获取用户

第一个示例演示了如何通过权限来检索用户,特别是可以编辑列表项的用户(使用

The first example demonstrates how to retrieve users by permission, in particular users who can edit list items (using PermissionKind.EditListItems):

using (var ctx = new ClientContext(webUri))
{
      //Retrieve site users             
      var users = ctx.LoadQuery(ctx.Web.SiteUsers.Where(u => u.PrincipalType == PrincipalType.User));
      ctx.ExecuteQuery();
      //Retrieve users permissions
      var userPermissionsResults = users.ToDictionary(user => user, user => ctx.Web.GetUserEffectivePermissions(user.LoginName));
      ctx.ExecuteQuery();
      //Filter the users who can edit list items      
      var usersCanEditListItems = new List<User>();
      foreach (var result in userPermissionsResults)
      {
          var user = result.Key;
          var userPermissions = result.Value.Value;
          if (userPermissions.Has(PermissionKind.EditListItems))
          {
             usersCanEditListItems.Add(user);
          }
      }
 }

示例2:按角色获取用户

角色类型或权限级别的情况下,该示例变得有点复杂,因为我们需要:

In case of role type or permission levels the example become a little more complicated since we need to:

  • 获取角色类型的权限列表(步骤1和2)
  • 获取具有权限的用户(第3步和第4步)
  • 按角色权限过滤用户(第5步)

示例:

using (var ctx = new ClientContext(webUri))
{

     //1.Retrieve role definition 
     var roleDef = ctx.Web.RoleDefinitions.GetByType(RoleType.Editor);
     ctx.Load(roleDef);
     ctx.ExecuteQuery();
     //2.Get permission levels for role 
     var permLevelNames = Enum.GetNames(typeof (PermissionKind));
     var permissionLevels = permLevelNames.Select(permLevelName => (PermissionKind) Enum.Parse(typeof (PermissionKind), permLevelName)).Where(permissionLevel => roleDef.BasePermissions.Has(permissionLevel)).ToList();

     //3.Retrieve users
     var users = ctx.LoadQuery(ctx.Web.SiteUsers.Where(u => u.PrincipalType == PrincipalType.User ));
     ctx.ExecuteQuery();
     //4.Retrieve users permissions
     var userPermissionsResults = users.ToDictionary(user => user, user => ctx.Web.GetUserEffectivePermissions(user.LoginName));
     ctx.ExecuteQuery();

     //5.Filter users by role
     var editorUsers = new List<User>();
     foreach (var result in userPermissionsResults)
     {
         var user = result.Key;
         var userPermissions = result.Value.Value;
         var hasPermissions = permissionLevels.All(userPermissions.Has); //has the same permissions?
         if (hasPermissions)
         {
             editorUsers.Add(user);
         }
     }
 }  

这篇关于在SharePoint 2013中使用CSOM根据特定权限获取所有用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆