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

标签 sharepoint sharepoint-2010 web-parts user-profile

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

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

感谢任何帮助。

最佳答案

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

关于sharepoint - 在 SharePoint 2010 中获取随机用户配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5096004/

相关文章:

c# - 向 Lambda 函数添​​加断点

c# - ASP.NET中如何在不手动刷新的情况下更新GridView中的数据?

sharepoint-2010 - 如何在 SharePoint 2010 中启用文档搜索?

sharepoint-2010 - SharePoint 2010 和 SAML 2.0

asp.net - ASP.NET 2.0 中的 @Register 指令

asp.net - Web部件中的Web浏览与个性化

c# - 使用 SharePoint 作为数据源

html - CSS 应用于默认菜单

c# - 获取 SPFieldCurrency 字段的数值

sharepoint - WebPart "metadata"?