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?

1

2 Answers 2

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;
    }
2
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/

1

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.