Set “User” type field

Posted: January 11, 2009 in Development, SharePoint
Tags:

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;
item.Update();
Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s