【备忘】根据User的Email获取SPUser

本文介绍了两种在SharePoint中检索用户的方法:使用SPWeb.AllUsers.GetByEmail和SPUtility.ResolvePrincipal。详细解释了SPWeb的Users、AllUsers及SiteUsers属性的区别,并指出了GetByEmail方法的一个明显缺陷。

1.  SPWeb.AllUsers.GetByEmail(string email)

using (SPSite spSite = new SPSite(spSiteUrl))
{
    using (SPWeb spWeb = spSite.RootWeb)
    {
        SPUser spUser = spWeb.AllUsers.GetByEmail(Email);
     }   
}

这种方式有一个明显的缺陷。

  分析一下SPWeb的三个属性AllUsers,SiteUsers, Users之间的区别:

  SPWeb.Users
  =============
  这个是所有直接加过权限的用户,就是add user时候不指定组,直接加个权限. 这些用户不属于任何站点的用户组
  (This represents the collection of users or user objects who have been explicitly assigned permissions in the Web site . This does not return users who have     access through a group).

  SPWeb.AllUsers
  =============
  Allusers = Users + AD Group.
  第一部分和上面一样,第二部分是这个用户登录过站点,但是他的权限是通过在SharePoint上给某个AD的group加权限获得的
  (This gives us the collection of user objects who are either members of the site collection or who have at least navigated to the site as authenticated members of a   domain group in the site).

  SPWeb.SiteUsers
  =============
  SiteUsers = AllUsers + SharePoint Group = Users + AD Group + SharePoint Group
  站点集中的所有的可用的用户, 包括WSS添加的隐藏的用户, 还包括通过SharePoint Group赋予权限的用户.
  (All the available users in the site collection, including some hidden users added by WSS. This is the collection of all users that belong to the site collection.)

  如果这个User存在,但是未登录过这个SPWeb,就会出现异常:“User cannot be found”。

 

2.  SPUtility.ResolvePrincipal (推荐)

// Add reference : Microsoft.SharePoint
using Microsoft.SharePoint.Utilities;

SPPrincipalInfo spPI = SPUtility.ResolvePrincipal(spSite.WebApplication, null, Email, SPPrincipalType.User, SPPrincipalSource.All, true);

if (spPI != null)
{
    SPUser spUser = spSite.RootWeb.AllUsers[spPI.LoginName]; 
}

 

转载于:https://www.cnblogs.com/landun/archive/2013/05/03/aaron.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值