Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3989624
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:23:48+00:00 2026-05-20T06:23:48+00:00

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

  • 0

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

                    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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T06:23:48+00:00Added an answer on May 20, 2026 at 6:23 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Iam trying to retrieve data from mysql database into stylesheet.php but it is not
I'm trying to retrieve N number of items from an XML file using simpleXML
I am trying retrieve user name from the code through graph api, the below
Hi I am trying to retrieve values from database. I have a row which
I am trying to retrieve a column from another related table, and display that
I'm trying to retrieve product information from magento by calling its web service using
I'm trying to retrieve the attribute value from an xsl:param and use it in
I'm trying to retrieve information(descriptionand Mail) from my active directory without knowing my DN
I'm trying to retrieve note titles from a specific notebook whenever the user clicks
I am trying to retrieve a few things from the database with just one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.