获取使用UserProfileManager一个随机的用户 [英] Getting a Random User using UserProfileManager
问题描述
我试图创建为SharePoint 2010项目我工作的一个谁是网络的一部分。
I'm trying to create a "Who is" web part for a SharePoint 2010 project i'm working on.
此Web部件应该选择从SharePoint配置文件的随机用户,并显示他/她的姓名,部门和电话。
This web part is supposed to select a random user from SharePoint profiles and display his/her name, department and phone.
问题是,我无法找到一种方法,直接从用户配置文件,这是我想要做什么得到一个随机用户。
The problem is that i couldn't find a way to get a random user directly from the User Profiles, which is what i'd like to do.
我找到了一种方法来做到这一点:
I found a way to do it:
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 +的用户,这就是为什么我想知道是否有可能直接从用户配置文件做到这一点。
This way i did it could be a problem because the site would have 2k+ users, that's why i'd like to know if it's possible to do this directly from the User Profiles.
我是新来的SharePoint,它仍然有点迷惑了我。
I'm new to SharePoint and it still a little confusing to me.
感谢您的帮助。
推荐答案
我很好奇,为什么需要它是一种随机的用户。我会建议使用建议周围同事OOB功能,并且您的Web部件可以改为公开此信息。
I'm curious why the need for it to be a "random" user. I would suggest using the OOB functionality around suggested colleagues, and your web part could expose this information instead.
这篇关于获取使用UserProfileManager一个随机的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!