4

I use the following code behind to update the user of a user field. The property allow multiple users is set for this field. How can I keep the users who are already present in this list and add the new user? (Now the field is cleared and refilled with the new value)

    
using (SPWeb Web = Site.RootWeb) {                  
SPUser user = Web.EnsureUser(UserName);
SPFieldUserValue fuv = new SPFieldUserValue(Web, user.ID, user.LoginName);
SPList List = Web.Lists["MyList"];
SPListItem item = List.GetItemById(ID);  

Web.AllowUnsafeUpdates = true;
item["MultipleUsersField"] = fuv;
item.Update();
Web.AllowUnsafeUpdates = false;

2 Answers 2

10

You can use this simple code:

SPFieldUserValueCollection values = (SPFieldUserValueCollection)item["MultipleUsersField"];
values.Add(new SPFieldUserValue(web, user.ID, user.Name));
item["MultipleUsersField"] = values;
0
0

See this question I answered the other day: Add users e-mail addresses from multivalue user field to string

I believe you can use that code provided in the link to retrieve the existing collection of users and then add or modify the collection how you please, then set that collection back as the value of the item.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.