自定义字段类型下拉列表sharepoint

时间:2009-06-24 05:40:08

标签: sharepoint

我创建了自定义字段类型,用于将可用用户列入Dropdownlist。我的Field类继承自SPFieldChoice和FieldControl类继承自DropDownChoiceField。 现在我已经在FieldControl类中编写了代码,

this.ddlInfoBox = new DropDownList();    
SPSite site = SPContext.GetContext(this.Context).Site;
SPWeb cWeb = SPContext.Current.Web;
Hashtable ht = new Hashtable();
SPUserCollection spUsers = cWeb.AllUsers;
foreach (SPUser item in spUsers)
{    
    SPFieldUser o= new SPFieldUser(
    ht.Add(item.Name, item);
}
this.ddlInfoBox.DataTextField = "Key";
this.ddlInfoBox.DataValueField = "Value";
this.ddlInfoBox.DataSource = ht;
this.ddlInfoBox.DataBind();

即。我正在尝试使用用户名呈现Dropdownlist。现在Value属性是,

public override object Value
{
    get
    {
        EnsureChildControls();                
        return this.ddlInfoBox.SelectedValue;
    }

    set
    {
        EnsureChildControls();                
        this.ddlInfoBox.SelectedValue = (string)this.ItemFieldValue;
    }
}

假设我已完成自定义字段类型,当我在名为“Temp”的列表中使用此字段并尝试创建列表项时,它会向我显示此站点的可用用户,当我创建项目时,数据将存储为所选用户的字符串,当我尝试将该列表视为Web部件时,将用户值过滤为[Me]。即分配给我的文件,它不允许我这样做,因为它是一个字符串值而不是SPFieldUser值。 如何将此字符串值存储为普通人和组开箱即用字段类型存储的SPFieldUser?

2 个答案:

答案 0 :(得分:1)

你不能从SPFieldUser派生而不是SPFieldChoice吗?而DropDownChoiceField使用UserField?

答案 1 :(得分:0)

SPUserCollection userscollection = rootWeb.SiteUsers;
SPUser user = userscollection.Web.EnsureUser("Domain\username");
SPFieldUserValue userval = 
   new SPFieldUserValue(user.ParentWeb, user.ID, user.LoginName);