sharepoint获取是否为输入域用户SharePoint PeopleEditor 控件的使用

 
简单的用法
复制代码
<%@ 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--允许为空的意思

获取用户的选择值

 

<SharePoint:PeopleEditor ID="people" runat="server" selectionset="User" MultiSelect="false" />

只选一个人的PeopleEditor控件

            List<string> disname = new List<string>();
             ArrayList list = PeopleEditor1.ResolvedEntities;
             //获取id和显示名称
             foreach (Microsoft.SharePoint.WebControls.PickerEntity p in list)
             {
                 string userId = p.EntityData["Department"].ToString();//属于哪个组
                 string DisplayName = p.DisplayText.ToString();//得到显示的用户名
                 disname.Add(DisplayName);
             }
             DropDownList1.DataSource = disname;//得到的用户绑定的DropDownList1中
             DropDownList1.DataBind();

 

 

 

 

    ArrayList list = PeopleEditor1.ResolvedEntities;
             //获取id和显示名称
             foreach (Microsoft.SharePoint.WebControls.PickerEntity p in list)
             {
                 string userId = p.EntityData["Department"].ToString();//隶属于哪个组
                 string DisplayName = p.DisplayText.ToString();//得到显示的用户名
             }

 

复制代码
 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
复制代码

 获取值并赋值给列表项it["审批人"]

<SharePoint:PeopleEditor ID="pplApprovers" width="300px" SelectionSet="User,DL,SecGroup,SPGroup" MultiSelect="true" AllowTypeIn="true" IsValid="true" runat="server" />

 SPWeb web = SPContext.Current.Web;
            //使用下面的代码保存数据到列表项:
            ArrayList approversArray = pplApprovers.ResolvedEntities;
            SPFieldUserValueCollection approversCollection = new SPFieldUserValueCollection();
            SPUser user;
            //SPGroup group;
          
               foreach (PickerEntity entity in approversArray)   
               {
                   //添加组
                   //group = web.SiteGroups[entity.Key];
                   //approversCollection.Add(new SPFieldUserValue(web, group.ID, group.LoginName));
                   //添加用户
                   user = web.EnsureUser(entity.Key);
                   approversCollection.Add(new SPFieldUserValue(web, user.ID, user.LoginName));
               }

it["审批人"] = approversCollection;

posted @ 2014-02-28 15:59  914556495  阅读(288)  评论(0编辑  收藏  举报