0

I update sharepoint list by code. In list I have people picker field. It allows multi userand SharePoint group also. Before allowing multi user and group it was allowed only single user. So I used below code amd it was worked fine.

SPUser user = web.EnsureUser("ApproverField");
listItem["Approver"]=user;
web.AllowUnsafeUpdates = true;
listItem.Update();

Now after changed to allow multi user and SharePoint group this code doesn't work. Exception fires at web.EnsureUser(). So can anyone suggest to make the change?

3

you can use the below function to store multiple users in People Picker

public SPFieldUserValueCollection GetSelectedUsers(PeopleEditor editor)
    {
        string selectedUsers = editor.CommaSeparatedAccounts;
        char[] splitter = { ',' };
        string[] splitPPData = selectedUsers.Split(splitter);
        SPFieldUserValueCollection values = new SPFieldUserValueCollection();
        for (int i = 0; i < splitPPData.Length; i++)
        {
            string loginName = splitPPData[i];
            if (!string.IsNullOrEmpty(loginName))
            {
                SPUser user = SPContext.Current.Web.SiteUsers[loginName];
                SPFieldUserValue fuv = new SPFieldUserValue(SPContext.Current.Web, user.ID, user.LoginName);
                fuv = new SPFieldUserValue(SPContext.Current.Web, user.ID, user.LoginName);
                values.Add(fuv);
                        }
                    }
                    return values;
    }
| improve this answer | |
1

Try following:

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

http://www.c-sharpcorner.com/uploadfile/dhananjaycoder/multiple-user-insertion-in-sharepoint-list/

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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