我已经要求将已保存的用户显示在SharePoint人员编辑器控件中。为此,我将用户名保存到People / Group列。我正在使用以下代码将这些用户带到人员编辑器控件:
SetPeopleEditor(item, Constants.FieldNames.IT_DIRECTOR, pe_ITDirector, oWeb);
上述方法的定义如下:
private PickerEntity SetPeopleEditor(SPListItem item, string columnName, PeopleEditor peopleEditor, SPWeb web)
{
ArrayList entityArrayList = new ArrayList();
PickerEntity entity = null;
if (item[columnName] != null)
{
char[] to_splitter = { ';' };
string to_list = item[columnName].ToString(); // Reads value stored in SPList. (i.e., "Domain\User1; Domain\User2")
string[] arr = to_list.Split(to_splitter);
string user = string.Empty;
for (int i = 1; i < arr.Length; i++)
{
if ((i % 2) != 0)
{
user = arr[i].Substring(arr[i].IndexOf("#") + 1);
entity = new PickerEntity();
entity.Key = user;
entity.IsResolved = true;
entity = peopleEditor.ValidateEntity(entity);
entityArrayList.Add(entity);
}
}
}
return entity;
}
但遗憾的是,控件始终显示空值。如何通过将数据填充到人员编辑器控件来实现此目的?
答案 0 :(得分:1)
您可以执行以下操作,
SPFieldUserValueCollection userValueCollection =
new SPFieldUserValueCollection(SPContext.Current.Web, SPContext.Current.Item["ColumnName"] as string);
if (userValueCollection .Count > 0)
{
spPeoplePickerContol.CommaSeparatedAccounts = userValueCollection[0].User.LoginName;
}
答案 1 :(得分:0)
在验证实体后,您必须包含peopleEditor.Entities.Add(实体)以将实体添加到peopleEditor控件。
private void SetPeopleEditor(SPListItem item, string columnName, PeopleEditor peopleEditor, SPWeb web)
{
if (item[columnName] != null)
{
char[] to_splitter = { ';' };
string to_list = item[columnName].ToString(); // Reads value stored in SPList. (i.e., "Domain\User1; Domain\User2")
string[] arr = to_list.Split(to_splitter);
string user = string.Empty;
for (int i = 1; i < arr.Length; i++)
{
if ((i % 2) != 0)
{
user = arr[i].Substring(arr[i].IndexOf("#") + 1);
PickerEntity entity = new PickerEntity();
entity.Key = user;
entity.IsResolved = true;
entity = peopleEditor.ValidateEntity(entity);
peopleEditor.Entities.Add(entity);
}
}
}
}
答案 2 :(得分:0)
试试这个:
string x = item["SP_Group"] == null ? "" : item["SP_Group"].ToString();
if (x != "")
{
SPFieldUserValue uv = new SPFieldUserValue(web, x);
SPGroup group = mySite.Groups.GetByID(uv.LookupId);
if (group.Users.Count > 0)
{
System.Collections.ArrayList entityArrayList = new System.Collections.ArrayList();
SPSecurity.RunWithElevatedPrivileges(delegate()
{
foreach (SPUser sUser in group.Users)
{
PickerEntity entity = new PickerEntity();
entity.Key = sUser.LoginName;
entity = peopleEditor.ValidateEntity(entity);
entityArrayList.Add(entity);
}
});
peopleEditor.UpdateEntities(entityArrayList);
peopleEditor.Validate();
}
else
{
peopleEditor.Entities.Clear();
}
}
string x = item["SP_Users"] == null ? "" : item["SP_Users"].ToString();
if (x != "")
{
SPFieldUserValueCollection uvcoll = new SPFieldUserValueCollection(mySite, x);
if (uvcoll.Count > 0)
{
System.Collections.ArrayList entityArrayList = new System.Collections.ArrayList();
SPSecurity.RunWithElevatedPrivileges(delegate()
{
foreach (SPFieldUserValue uv in uvcoll)
{
SPUser sUser = uv.User;
PickerEntity entity = new PickerEntity();
entity.Key = sUser.LoginName;
entity = peopleEditor.ValidateEntity(entity);
entityArrayList.Add(entity);
}
});
peopleEditor.UpdateEntities(entityArrayList);
peopleEditor.Validate();
}
else
{
peopleEditor.Entities.Clear();
}
}