views:

57

answers:

1

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.

A: 

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.

Doug Stalter
Doug, the random part is just to show a random employee when you enter the page, it's just a little detail that was asked for me to do :/
C.Hoffmann