I am trying to create an Whoβs Web Part for the SharePoint 2010 project I'm working on.
This web part should select a random user from the SharePoint profiles and display his / her name, department and phone.
The problem is that I could not find a way to get a random user directly from user profiles, which I would like to do.
I found a way to do this:
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; } }
Thus, I did this can be a problem, because the site will have 2k + users, so I would like to know if it is possible to do this directly from user profiles.
I'm new to SharePoint and it still confuses me a bit.
Thank you for your help.
source share