Those have spent hour to figure why the list does not store login name directly will know extra effort is required to get / set username from / to user field. In fact, it is stored in the following format:
ID ;# account name
In this case, you need the following code snippet to insert very into user field with the help of SPFieldUserValue
using (SPSite site = new SPSite("http://portal"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["Example For Users"];
SPListItem item = list.Items[0];
SPFieldUserValue userValue = new SPFieldUserValue(web, web.CurrentUser.ID, web.CurrentUser.LoginName);
item["User Name"] = userValue;
item.Update();
}
}
Wednesday, April 22, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment