最近一段时间,项目用到了PoopleEditor控件,下面的资料都是从网上收集过来并做一些整理
简单的用法
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Import Namespace="Microsoft.SharePoint" %> <SharePoint:PeopleEditor id="PeopleEditor1" runat="server" SelectionSet="User" ValidatorEnabled="true" AllowEmpty = "false" MultiSelect = "true" />
上面是SharePoint 2007的用法,一般要注册控件
SelectionSet---是可以先择的范围,一般可以是组,人,及Ad的安全组等
ValidatorEnable----如果设置为True,其会自动验证,当刷新的时候
AllowsEmpty--允许为空的意思
获取用户的选择值
ArrayList list = PeopleEditor1.ResolvedEntities ; //获取id和显示名称 foreach (Microsoft.SharePoint.WebControls.PickerEntity p in list) { string userId = p.EntityData["SPUserID"].ToString(); string DisplayName = p.EntityData["DisplayName"].ToString(); } //获取帐号 ArrayList selectedAccoutList = PeopleEditor1.Accounts; string selectedAccouts2 = PeopleEditor1.CommaSeparatedAccounts;
设置控件的值
PeopleEditor1.CommaSeparatedAccounts = @"JYSERVER\spsadmin,JYSERVER\administrator";
如果是允许多个值,那么可以把放到一个集合里面,如下
string[] user = userPicker.CommaSeparatedAccounts.Split(new string[] { "," }, StringSplitOptions.None)
SharePoint 2010可以引用以下DLL文件
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
--Note:其实在SharePoint 2010 里面可以引用07上段的注册,在开发中一样可以用!
较复杂的用法
下面是一个可以允许组,安全组,人的实例,允许多选!
<SharePoint:PeopleEditor ID="pplApprovers" width="300px" SelectionSet="User,DL,SecGroup,SPGroup" MultiSelect="true" AllowTypeIn="true" IsValid="true" runat="server" />
设置值
private ArrayList approversArray; private SPFieldUserValueCollection approversCollection; approversCollection = (SPFieldUserValueCollection)item[“Approvers”]; PickerEntity entity = new PickerEntity(); approversArray = new ArrayList(); foreach (SPFieldUserValue spFieldUserValue in approversCollection) { entity = new PickerEntity(); entity.Key = spFieldUserValue.LookupValue; entity = pplApprovers.ValidateEntity(entity); approversArray.Add(entity); } pplApprovers.UpdateEntities(approversArray);
取值
approversArray = pplApprovers.ResolvedEntities; //Use the following code to save the data back to the list item: approversCollection = new SPFieldUserValueCollection(); SPUser user; SPGroup group; foreach (PickerEntity entity in approversArray) { if (entity.EntityData["PrincipalType"].ToString() == "SharePointGroup") { group = web.SiteGroups[entity.Key]; approversCollection.Add(new SPFieldUserValue(web, group.ID, group.LoginName)); } else { //handles SecurityGroup, Distribution List and User user = web.EnsureUser(entity.Key); approversCollection.Add(new SPFieldUserValue(web, user.ID, user.LoginName)); } } item[“Approvers”] = approversCollection;
UI设置方面
一般来说,如果其允许多个选择的时候,其会很比较宽,同时其会比较大,而且选择的按钮也是显示在右下角,当我们用在查询的时候,就会显示得很不好看,所以我们要限制其高度等其他方面让其显示得漂亮一点。下面是我用到的一个可以多重选择的,显示在一行的实例。
<td style="width:400px; text-align:left; height:28px; line-height:28px;"> <SharePoint:PeopleEditor ID="pdName" SelectionSet="User,SPGroup" MultiSelect="true" runat="server" Rows="1" Width="100%" PlaceButtonsUnderEntityEditor="false" /> </td>
其中
PlaceButtonsUnderEntityEditor="false" 让浏览按钮设置右边
Rows="1" 意思只高度只有一行。
一般来说可以设置其BorderColor="Gray" BorderWidth="1“ 这样子还是挺好看的!
最好设置ValidatorEnabled="true"
上面是我用到查询出报表中,其可以参考上面的复杂用法取值,如我项目中这样做的
List<string> userlist = new List<string>(); #region 得到用户列表 SPWeb web = SPContext.Current.Web; ArrayList approversArray = pdName.ResolvedEntities; foreach (PickerEntity entity in approversArray) { if (entity.EntityData["PrincipalType"].ToString() == "SharePointGroup") { SPGroup group = web.SiteGroups[entity.Key]; grouplist.Add(group); } else { SPUser user = web.EnsureUser(entity.Key); userlist.Add(user.LoginName); } } foreach (SPGroup g in grouplist) { SPUserCollection users = g.Users; foreach (SPUser u in users) { if (!userlist.Contains(u.LoginName)) { userlist.Add(u.LoginName); } } } #endregion
其他的如Secroup和DL 暂时不知道怎么取,可以较复杂的那种 可以取出来,等用到的时候再填充!
下面是参考的地址:
http://blogs.catapultsystems.com/jdunagan/archive/2011/08/24/sharepointpeopleeditor-basics.aspx
http://www.cnblogs.com/jianyi0115/archive/2007/11/10/955452.html