2010-09-02 49 views
4

我正在嘗試爲正在處理的SharePoint 2010項目創建「Who is」Web部件。使用UserProfileManager獲取隨機用戶

此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,它仍然有點令我困惑。

感謝您的幫助。

回答

0

我很好奇爲什麼需要它成爲一個「隨機」用戶。我建議圍繞建議的同事使用OOB功能,而您的Web部分可能會公開這些信息。

+0

Doug,隨機部分僅僅是當你進入頁面時顯示一個隨機的員工,這只是一個被要求我做的細節:/ – 2010-09-02 16:52:38