2

I've a requirement in which I've to display the saved users to the SharePoint people editor control. For this, I am saving the user names to People/Group column. And I am using the following code for taking these users to people editor control:

SetPeopleEditor(item, Constants.FieldNames.IT_DIRECTOR, pe_ITDirector, oWeb);

the definition of the above method is shown below:

        private PickerEntity SetPeopleEditor(SPListItem item, string columnName, PeopleEditor peopleEditor, SPWeb web)
    {
        ArrayList entityArrayList = new ArrayList();
        PickerEntity entity = null;
        if (item[columnName] != null)
        {
            char[] to_splitter = { ';' };
            string to_list = item[columnName].ToString(); // Reads value stored in SPList. (i.e., "Domain\User1; Domain\User2")
            string[] arr = to_list.Split(to_splitter);
            string user = string.Empty;
            for (int i = 1; i < arr.Length; i++)
            {
                if ((i % 2) != 0)
                {
                    user = arr[i].Substring(arr[i].IndexOf("#") + 1);
                    entity = new PickerEntity();
                    entity.Key = user;
                    entity.IsResolved = true;
                    entity = peopleEditor.ValidateEntity(entity);
                    entityArrayList.Add(entity);
                }
            }


        }
        return entity;
    }

But unfortunately, the control always showing empty value. How can I achieve this by populating data to the people editor control?

love thakker
  • 460
  • 2
  • 13
  • 29

3 Answers3

1

You can do as follows,

SPFieldUserValueCollection userValueCollection =
             new SPFieldUserValueCollection(SPContext.Current.Web, SPContext.Current.Item["ColumnName"] as string);
        if (userValueCollection .Count > 0)
        {
            spPeoplePickerContol.CommaSeparatedAccounts = userValueCollection[0].User.LoginName;
        }
Jatin patil
  • 4,252
  • 1
  • 18
  • 27
0

You have to include peopleEditor.Entities.Add(entity) after validating entity to add the entity to peopleEditor control.

 private void SetPeopleEditor(SPListItem item, string columnName, PeopleEditor peopleEditor, SPWeb web)
        {
            if (item[columnName] != null)
            {
                char[] to_splitter = { ';' };
                string to_list = item[columnName].ToString(); // Reads value stored in SPList. (i.e., "Domain\User1; Domain\User2")
                string[] arr = to_list.Split(to_splitter);
                string user = string.Empty;
                for (int i = 1; i < arr.Length; i++)
                {
                    if ((i % 2) != 0)
                    {
                        user = arr[i].Substring(arr[i].IndexOf("#") + 1);
                       PickerEntity entity  = new PickerEntity();
                        entity.Key = user;
                        entity.IsResolved = true;
                        entity = peopleEditor.ValidateEntity(entity);
                        peopleEditor.Entities.Add(entity);
                    }
                }
            }
        }
Unnie
  • 918
  • 8
  • 30
0

Try this:


string x = item["SP_Group"] == null ? "" : item["SP_Group"].ToString();

                if (x != "")
                {
                    SPFieldUserValue uv = new SPFieldUserValue(web, x);
                    SPGroup group = mySite.Groups.GetByID(uv.LookupId);

                    if (group.Users.Count > 0)
                    {
                        System.Collections.ArrayList entityArrayList = new System.Collections.ArrayList();
                        SPSecurity.RunWithElevatedPrivileges(delegate()
                        {
                            foreach (SPUser sUser in group.Users)
                            {
                                PickerEntity entity = new PickerEntity();
                                entity.Key = sUser.LoginName;
                                entity = peopleEditor.ValidateEntity(entity);
                                entityArrayList.Add(entity);
                            }

                        });
                        peopleEditor.UpdateEntities(entityArrayList);
                        peopleEditor.Validate();
                    }
                    else
                    {
                        peopleEditor.Entities.Clear();
                    }

                }

string x = item["SP_Users"] == null ? "" : item["SP_Users"].ToString();

                if (x != "")
                {
                    SPFieldUserValueCollection uvcoll = new SPFieldUserValueCollection(mySite, x);                       

                    if (uvcoll.Count > 0)
                    {
                        System.Collections.ArrayList entityArrayList = new System.Collections.ArrayList();
                        SPSecurity.RunWithElevatedPrivileges(delegate()
                        {
                            foreach (SPFieldUserValue uv in uvcoll)
                            {
                                SPUser sUser = uv.User;
                                PickerEntity entity = new PickerEntity();
                                entity.Key = sUser.LoginName;
                                entity = peopleEditor.ValidateEntity(entity);
                                entityArrayList.Add(entity);
                            }

                        });
                        peopleEditor.UpdateEntities(entityArrayList);
                        peopleEditor.Validate();
                    }
                    else
                    {
                        peopleEditor.Entities.Clear();
                    }

                }