有时候页面是要放一个PeopleEditor,然后赋值给item。当然最开始的时候想法应该是超级简单,直接把控件的value赋值给item就完事了。结果根本就不是那么一回事。
网上有人提供了方法,写了一大堆,很复杂,两眼没看懂,又感觉怎么不像自己要的。没办法,还是要自己弄一下。
不过还是要承认借鉴了网上通用的方法,只是在SharePoint2010有点变化。
页面加控件:
<SharePoint:PeopleEditor ID="people" runat="server" selectionset="User" MultiSelect="false" />
只选一个人的PeopleEditor控件
下面的方法是拿控件里的值。和网上sharepoint2007版本的有点不同,在string DisplayName行
public static string GetPeopleEditorValue(PeopleEditor objPeopleEditor)
{
string strResult = string.Empty;
ArrayList list = objPeopleEditor.ResolvedEntities;
foreach (Microsoft.SharePoint.WebControls.PickerEntity p in list)
{
string userId = p.EntityData["SPUserID"].ToString();
string DisplayName = p.DisplayText.ToString();
strResult += userId + ";#" + DisplayName;
strResult += ",";
}
return strResult;
}
赋值:
SPContext.Current.ListItem["人"] = GetPeopleEditorValue(people);
如果PeopleEdit里面没有选人,则是空值,ListItem["人"]就是空值,不会报什么错误的。
----------
注意,上面的方法不是很好,当这个用户从来没有登录到SharePont上,那么这个方法是会报错误的。这种情况发生在测试阶段就会暴露出来。
要用下面的方法
public string GetPersonInfo(Control control, string ffID) { string strReturn = ""; if (control is PeopleEditor) { if (((PeopleEditor)control).Parent.Parent.Parent.ID == ffID) { PeopleEditor peopleEditor = ((PeopleEditor)control); foreach (PickerEntity pe in peopleEditor.ResolvedEntities) { string principalType = pe.EntityData["PrincipalType"].ToString(); if (principalType == "User" || principalType == "SecurityGroup") { strReturn = pe.Key; break; } } } } else { foreach (Control c in control.Controls) { strReturn = GetPersonInfo(c, ffID); if (strReturn != "") { break; } } } return strReturn; }
有取值就要有赋值
public void SetDefaultPerson(Control control, string ffID, string userLoginName) { string strflag = ""; if (control is PeopleEditor) { if (((PeopleEditor)control).Parent.Parent.Parent.ID == ffID) { if ((((PeopleEditor)control).ResolvedEntities.Count < 1)) { PickerEntity entity = new PickerEntity(); entity.Key = userLoginName; System.Collections.ArrayList entityArrayList = new System.Collections.ArrayList(); entityArrayList.Add(entity); ((PeopleEditor)control).UpdateEntities(entityArrayList); strflag = "TaskOver"; } } } else { foreach (Control c in control.Controls) { SetDefaultPerson(c, ffID, userLoginName); if (strflag != "") { break; } } } }