惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Blog of Author Tim Ferriss
TaoSecurity Blog
TaoSecurity Blog
Apple Machine Learning Research
Apple Machine Learning Research
Hugging Face - Blog
Hugging Face - Blog
IT之家
IT之家
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
小众软件
小众软件
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
SegmentFault 最新的问题
T
Troy Hunt's Blog
N
News and Events Feed by Topic
雷峰网
雷峰网
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
PCI Perspectives
PCI Perspectives
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
www.infosecurity-magazine.com
www.infosecurity-magazine.com
博客园 - 三生石上(FineUI控件)
Schneier on Security
Schneier on Security
T
The Exploit Database - CXSecurity.com
L
LINUX DO - 最新话题
V
V2EX
T
Threat Research - Cisco Blogs
人人都是产品经理
人人都是产品经理
C
Cisco Blogs
The GitHub Blog
The GitHub Blog
爱范儿
爱范儿
I
Intezer
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recent Announcements
Recent Announcements
月光博客
月光博客
Recent Commits to openclaw:main
Recent Commits to openclaw:main
N
News | PayPal Newsroom
Cyberwarzone
Cyberwarzone
B
Blog
博客园 - 聂微东
P
Palo Alto Networks Blog
A
About on SuperTechFans
The Last Watchdog
The Last Watchdog
Scott Helme
Scott Helme
Google DeepMind News
Google DeepMind News
Webroot Blog
Webroot Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
O
OpenAI News
C
Check Point Blog
Hacker News: Ask HN
Hacker News: Ask HN
W
WeLiveSecurity
V
Vulnerabilities – Threatpost
博客园 - 【当耐特】
有赞技术团队
有赞技术团队
A
Arctic Wolf

博客园 - 名可真难起

CSOM创建Content Type并指定GUID SharePoint还原大体积内容数据库报空间错误的解决办法 SharePoint 2013: Knockout JS + Rest Service展示List数据 SharePoint 2013—创建Master Page 如何在页面中使用sharepoint中的富文本编辑器控件 通过编程的方式给列表项添加附件 SharePoint 2010 Visual Web Part发布后找不到文件的错误 SharePoint 2010通过Web Service读取和修改Excel文件 SharePoint 2010中Metadata Service 解决SharePoint2010中Excel刷新出错的问题 SharePoint 2007搜索网站开启匿名访问 SharePoint 2010: 取得浏览最多的网站 在Windows 7中如何用另一个用户运行程序 不仅仅是土豆 SharePoint 2010运行环境 最近比较懒 如何在ActionResult中返回XML类型 - 名可真难起 - 博客园 SharePoint 2010 Development Stack SharePoint 2010 Visual Web Part Demo--展示网站中的List.
通过SPWeb.EnsureUser将SPUser添加到网站
名可真难起 · 2011-05-03 · via 博客园 - 名可真难起

最近在写代码时发现,通过Web.Users, Web.AllUsers,web. SiteUsers取得用户信息时经常找不到用户。

在项目中,域中有个组叫UK,这个组里有20多个人。当把UK这个组加到网站的访问者之后,UK组中的人都是可以通过自己的域账户来访问网站了。在代码中要取得用户的邮件,于是想到了用Web.Users来取得用户信息。但是通过调试发现,有几个用户总取不到信息,提示“未找到用户”。

取得用户信息可以用下面的语句:

SPWeb web = SPContext.Current.Web;

SPUser user = web. Users [“Domain\Username”];

如果该语句运行正常,就可以获取一个user对象。不幸的是,有几个用户总是找不到。于是尝试下面的语句,但结果同样是找不到用户。

SPUser user = web. AllUsers [“Domain\Username”];

SPUser user = web. SiteUsers [“Domain\Username”];

以下是MSDN对这三种方法的解释:

SPWeb.AllUsers
属性 (Microsoft.SharePoint)

Gets the collection of user objects that represents all users who are
either members of the site or who have browsed to the site as authenticated
members of a domain group in the site.

SPWeb.SiteUsers
属性 (Microsoft.SharePoint)

Gets the collection of all users that belong to the site collection.

SPWeb.Users
属性 (Microsoft.SharePoint)

Gets the collection of user objects that are explicitly assigned
permissions in the Web site. 

由此可见SPWeb.AllUsers取得的范围最大。根据我个人的理解要想成为members of the site,需要将该用户直接加入到网站中某一个用户组中,而不是加入其所在的域的组。所以此时即使用SPUser user = web. AllUsers [“Domain\Username”];也同样找不到用户。

解决的办法是用SPWeb.EnsureUser来检测一下用户是否存在。如果该用户不存在,这个方法会将该用户加到网站中。

SPWeb.EnsureUser
方法 (Microsoft.SharePoint)

Checks whether the specified login name belongs to a valid user of the Web
site, and if the login name does not already exist, adds it to the Web site. 

至此问题解决。

致力于SharePoint开发. QQ:28748451.