2

我有一个要求,我必须将保存的用户显示到 SharePoint 人员编辑器控件。为此,我将用户名保存到 People/Group 列。我正在使用以下代码将这些用户带到人员编辑器控制中:

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

上述方法的定义如下所示:

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

但不幸的是,控件总是显示空值。如何通过将数据填充到人员编辑器控件来实现此目的?

4

3 回答 3

1

您可以执行以下操作,

SPFieldUserValueCollection userValueCollection =
             new SPFieldUserValueCollection(SPContext.Current.Web, SPContext.Current.Item["ColumnName"] as string);
        if (userValueCollection .Count > 0)
        {
            spPeoplePickerContol.CommaSeparatedAccounts = userValueCollection[0].User.LoginName;
        }
于 2014-03-03T09:29:54.193 回答
0

您必须在验证实体后包含 peopleEditor.Entities.Add(entity) 才能将实体添加到 peopleEditor 控件。

 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);
                    }
                }
            }
        }
于 2014-03-03T16:19:39.153 回答
0

尝试这个:


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();
                    }

                }
于 2014-10-20T11:56:37.167 回答