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);
                }
            }

上記のコードに「forループ」の代わりに単純なforeachを適用すると、コードが機能します。

    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

ランダムをより適切に処理するコード

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 に答える