开发者

Getting a random UserProfile In SharePoint 2010

开发者 https://www.devze.com 2023-02-13 04:46 出处:网络
I am trying to retrieve a random number of users from the UserProfileManager. But I am encountering errors when deploying to the live servers. I can\'t seem to see what is causing the error. My code

I am trying to retrieve a random number of users from the UserProfileManager.

But I am encountering errors when deploying to the live servers. I can't seem to see what is causing the error. My code is below:

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

                 开发者_Python百科   drUserProfile["MySiteUrl"] = up.PublicUrl;

                    dtUserProfile.Rows.Add(drUserProfile);
                }
            }

My code works when I apply a simple foreach to my code above instead of the "for loop":

    foreach (UserProfile up in profileManager)

Which proves I can return userprofiles.

Any help is appreciated.


profileManager.GetUserProfile(long recordId) 

expects a recordId from userprofile table. It is not an index, so you cannot use "random".

If you want to check RecordId, you can take a look at SQL tables of ProfileDB. Table "UserProfile_Full" has MasterRecordId column. Your parameter in GetUserProfile has to match of the user profile's MasterRecordId.

you can use the following code to get your random profiles:

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

UserProfile currentProfile = (UserProfile)profiles.Current


Code that handles Random better

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...

   }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号