要在MOSS中开发一个有用户选择功能的页面或webpart,就要用到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"
/>
这样,就能显示出标准的用户选择界面了.

接下来,我们如何获取选择的用户数据呢?
 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";

测试代码需要在moss的环境中运行,页面放入_layouts文件夹,或写到webpart中。

下面是一个完整的测试用webpart:
//test code by jianyi0115@163.com
// 2008-11-10
//
//
using System;
using System.Collections;
using System.Text;
using sharepoint = Microsoft.SharePoint.WebControls ;
using wss = Microsoft.SharePoint.WebPartPages;
using System.Web.UI.WebControls  ;
using System.Runtime.InteropServices;
using Microsoft.SharePoint;

namespace PeoplePickerTest
{
    [Guid(
"8D64A4D3-8C9F-4a44-9720-611A701C0B79")]
    
public class MyPeoplePicker : wss.WebPart                
    {
        
private Microsoft.SharePoint.WebControls.PeopleEditor PeopleEditor1;
        
private Button btn;

        
protected override void CreateChildControls()
        {
            
base.CreateChildControls();
            PeopleEditor1 
= new Microsoft.SharePoint.WebControls.PeopleEditor();
            Controls.Add(PeopleEditor1);
            btn 
= new Button();
            btn.Text 
= "test";
            Controls.Add(btn);
            btn.Click 
+= new EventHandler(btn_Click);
         }
 
        
void btn_Click(object sender, EventArgs e)
        {
            Page.Response.Write( PeopleEditor1.CommaSeparatedAccounts );

            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";
        }
    }
}

posted on 2007-11-10 20:46  jianyi  阅读(2445)  评论(9编辑  收藏  举报