使用UserProfileManager获取随机用户

我正在尝试为我正在开发的SharePoint 2010项目创建“Who is”Web部件。

此Web部件应该从SharePoint配置文件中选择一个随机用户,并显示他/她的姓名,部门和电话。

问题是我无法找到直接从用户配置文件中获取随机用户的方法,这就是我想要做的。

我找到了一种方法:

SPServiceContext myContext = SPServiceContext.GetContext(mySite); SPWeb myWeb = SPContext.Current.Web; UserProfileManager profileManager = new UserProfileManager(myContext); bool boolOut; SPPrincipalInfo[] userInfos = SPUtility.GetPrincipalsInGroup(myWeb, "AllUsers", profileManager.Count, out boolOut); Random random = new Random(); int randomUser = random.Next(0, userInfos.Length); SPPrincipalInfo user = userInfos[randomUser]; bool userFound = false; while(!userFound) { if (profileManager.UserExists(user.LoginName)) { UserProfile userProfile = profileManager.GetUserProfile(user.LoginName); userDepartment = Convert.ToString(userProfile[PropertyConstants.Department].Value); userPicture = Convert.ToString(UserProfile[PropertyConstants.PictureUrl].Value); userFound = true; } } 

这样我可能会遇到问题,因为该网站会有2k +用户,这就是为什么我想知道是否可以直接从用户配置文件中执行此操作。

我是SharePoint的新手,对我来说仍然有点混乱。

谢谢你的帮助。

我很好奇为什么需要它成为“随机”用户。 我建议在建议的同事周围使用OOBfunction,而您的Web部件可能会公开此信息。