3

我正在尝试从 UserProfileManager 中检索随机数量的用户。

但是在部署到实时服务器时遇到错误。我似乎看不出是什么导致了错误。我的代码如下:

for (int i = 0; i < NumberOfUserLimit; i++)
            {
                UserProfile up = profileManager.GetUserProfile(random.Next(1, NumberOfUserLimit));

                if (up["FirstName"] != null && up["FirstName"].Value != null && !String.IsNullOrEmpty(up["FirstName"].Value.ToString()))
                {
                    DataRow drUserProfile;

                    drUserProfile = dtUserProfile.NewRow();

                    drUserProfile["DisplayName"] = up.DisplayName;
                    drUserProfile["FirstName"] = up["FirstName"].Value;
                    drUserProfile["LastName"] = up["LastName"].Value;
                    drUserProfile["Department"] = up["Department"].Value;
                    drUserProfile["Location"] = up["SPS-Location"].Value;
                    drUserProfile["HireDate"] = up["SPS-HireDate"].Value;
                    drUserProfile["ContactNumber"] = up["Office"].Value;

                    if (up["PictureURL"] != null && up["PictureURL"].Value != null && !String.IsNullOrEmpty(up["PictureURL"].Value.ToString()))
                    {
                        string cleanAccountName = up["AccountName"].Value.ToString().Replace(@"\", "_");
                        string pictureUrl = String.Format("https://my.someintranet.com/User Photos/Profile Pictures/{0}_MThumb.jpg", cleanAccountName);

                        drUserProfile["Image"] = pictureUrl;
                    }
                    else
                    {
                        drUserProfile["Image"] = "~/_layouts/images/O14_person_placeHolder_96.png";
                    }

                    drUserProfile["MySiteUrl"] = up.PublicUrl;

                    dtUserProfile.Rows.Add(drUserProfile);
                }
            }

当我对上面的代码应用一个简单的 foreach 而不是“for 循环”时,我的代码可以工作:

    foreach (UserProfile up in profileManager)

这证明我可以返回用户配置文件。

任何帮助表示赞赏。

4

2 回答 2

2
profileManager.GetUserProfile(long recordId) 

需要来自 userprofile 表的 recordId。它不是索引,因此您不能使用“随机”。

如果要查看 RecordId,可以查看 ProfileDB 的 SQL 表。表“UserProfile_Full”具有 MasterRecordId 列。GetUserProfile 中的参数必须与用户配置文件的 MasterRecordId 匹配。

您可以使用以下代码获取随机配置文件:

IEnumerator profiles = profileManager.GetEnumerator(); 
int index = new Random().Next(1, 100); 
while (index >= 0 && profiles.MoveNext()) 
   index--; 

UserProfile currentProfile = (UserProfile)profiles.Current
于 2011-02-23T19:47:55.450 回答
1

更好地处理 Random 的代码

public class TestClass
{
   private random = new Random();
   private long totalNumberOfProfiles;   //ProfileManager.Count not always returns count correctly

   public TestClass()
   {
      //this does not have to be in constructor but point is to have it cached (reasonably)
      IEnumerator profiles = profileManager.GetEnumerator(); 
      long counter = 0;
      while (profiles.MoveNext())
         counter++;
      this.totalNumberOfProfiles = counter;
   }


   public fillInDataSet()
   {
      //something is here...
      IEnumerator profiles = profileManager.GetEnumerator(); 
      int index = random.Next(1, totalNumberOfProfiles); 
      while (index >= 0 && profiles.MoveNext()) 
        index--; 

      UserProfile currentProfile = (UserProfile)profiles.Current

      //something is here...

   }
}
于 2011-02-24T23:42:12.487 回答