1
votes

I have created my custom field type for listing available users in to Dropdownlist. My Field class inherit from SPFieldChoice and FieldControl class inherit from DropDownChoiceField. Now I have written code inside FieldControl class as,

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();

i.e. I am trying to render the Dropdownlist with user names. Now Value property is,

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

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

Let say I am done with custom field type, when I use this field in a list named "Temp" and tries to create a list item it shows me the available users for this site, when I create an item the data will be stored as string for selected user and when I tries to View that list as a web part with filter the user value as [Me]. i.e the documents assigned to me, it did not let me to do because it is a string value not a SPFieldUser value. How can I store this string value as a SPFieldUser that normal people and group Out of the box field type stores?

2
Why are you calling EnsureChildControls from your Value property? Be very careful as this may interfere with the ASP.NET page lifecycle.Alex Angas

2 Answers

1
votes

Can't you derive from SPFieldUser instead of SPFieldChoice? And for DropDownChoiceField use UserField?

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