Hello, I'm trying to create a "Who is" web part for a SharePoint 2010 project i'm working on.
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;
}
}
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.
I'm new to SharePoint and it still a little confusing to me.
Thanks for your help.