tags:

views:

1171

answers:

4

Hi Everybody,

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?

+1  A: 

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

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

Thanks for the reply,

Could anyone please let me know what code will be there inside the Property Value i.e get and set so that the list item data could be stored as a SPFieldUser and I can filter the data using this field control by using [Me]

Thanks

Tejas
A: 

Anyone have any updates on this?

Thanks, Tejas

Tejas