How to update a people and group field via code?

Posted: April 21, 2009 in Development, SharePoint
Tags: ,

Today I was working on my custom web part. One of my questions was : after pressing submit button how to update a people and group field in the destination list.

I tested many ways and they gives me this error :

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

after searching and googling for some hours I found it in my way , here it is :


SPListItemCollection itemCollection;
SPListItem item;
PeopleEditor people;
PickerEntity picker;
SPUserInfo userInf;
SPUser user;

itemCollection = web.Lists["ListName"].Items;
item = itemCollection.Add();

// I arranged my controls in a table
// here is my one of people editor controls
// So after pressing submit, the account of this control should set
// new Item "Assigned To" field in the related list.
people = (PeopleEditor)Tbl.Rows[0].Cells[0].Controls[0];  
picker = (PickerEntity)people.ResolvedEntities[0];
userInf = new SPUserInfo();
user = web.EnsureUser(picker.Key);
item["Assigned To"] = user;
item.Update();

with this kind of coding my list start updating correctly 🙂 ! hope this help you …

Also here is some related links that helped me :

Advertisements
Comments
  1. Kaustubh Wadi says:

    Thanks SNR.
    This helps me a lot.

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