在 SharePoint 2010 中获取随机用户配置文件

发布于 2024-10-19 06:53:49 字数 2076 浏览 7 评论 0原文

我正在尝试从 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)

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

任何帮助表示赞赏。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

佞臣 2024-10-26 06:53:49
profileManager.GetUserProfile(long recordId) 

需要来自用户配置文件表的 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
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
苄①跕圉湢 2024-10-26 06:53:49

更好地处理随机的代码

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

   }
}

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

   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文