Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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;
share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted

You can use this simple code:

SPFieldUserValueCollection values = (SPFieldUserValueCollection)item["MultipleUsersField"];
values.Add(new SPFieldUserValue(web, user.ID, user.Name));
item["MultipleUsersField"] = values;
share|improve this answer
    
That's what I was looking for! Thanks Vedran! –  Wim Hill Sep 5 '11 at 13:55
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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