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