Set “User” type field

Posted: January 11, 2009 in Development, SharePoint

You may have seen this error message if you have tried to update a “People and Group” or “User” field by a simple string ( like “Domain\Username” ).

Invalid data has been used to update
the list item. The field you are trying to update may be read only.

I found that you should use SPUser field type in this situation. this example could help you if you have encountered this problem :

SPSite site;
SPWeb web;
SPListItemCollection listItems;
SPListItem item;
SPUser currentUser;
site = SPContext.Current.Site;
web = site.OpenWeb();
currentUser = web.CurrentUser; OR web.EnsureUser("Domain\Username");
listItems = web.Lists["WorkTimes"].Items;
item = listItems.Add();
item["User"] = currentUser;

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s