我无法在sharepoint 2013人员选择器字段中保存asp.net(应用程序页面)人员编辑器控件值

时间:2014-12-26 06:01:11

标签: sharepoint-2013

我在sharepoint 2013列表中创建了人员或组列,并且我已经在share环境中为sharepoint 2013创建了asp.net应用程序页面。问题是当我试图保存人员时,我已将人员编辑器控件置于应用程序页面中编辑器值为sharepoint人或组(人员选择器字段)...我得到错误像字符串到SPUser转换是不可能的...你能给出建议来解决这个...谢谢

1 个答案:

答案 0 :(得分:0)

  var peoplePicker = this."WhereYourControlAdded_Id".FindControl("PeoplePickerControlID") as PeopleEditor;
  //here you should find your people picker control


   PeopleEditor peoplePicker = value as PeopleEditor;
   if (peoplePicker != null && peoplePicker.CommaSeparatedAccounts.ToString() != "")
   {
    string[] userarray = peoplePicker.CommaSeparatedAccounts.ToString().Split(',');
    SPFieldUserValueCollection usercollection = new SPFieldUserValueCollection();

      for (int i = 0; i < userarray.Length; i++)
      {
          SPFieldUserValue usertoadd = ConvertLoginAccount(userarray[i]);
          usercollection.Add(usertoadd);
      }

        item[item.Fields[columnName].InternalName] = usercollection;
    }
    else
    {
        item[item.Fields[columnName].InternalName] = null;
    }




 public static SPFieldUserValue ConvertLoginAccount(string userid)
    {
        SPFieldUserValue uservalue = null;
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {

            using (SPSite site = new SPSite(SPContext.Current.Site.Url))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPUser requireduser = web.EnsureUser(userid);
                    uservalue = new SPFieldUserValue(web, requireduser.ID, requireduser.LoginName);
                }
            }

        });
        return uservalue;
    }