2014-12-26 50 views
0

我在SharePoint 2013列表中創建了人員或組列,並且我在vs環境中創建了asp.net應用程序頁面for sharepoint 2013.問題是,當我試圖將人員編輯器值保存到sharepoint person或group(人員選擇器字段)時,我將人員編輯器控件放在了apllication頁面中...我得到了像String到SPUser轉換的錯誤是不可能的...請你給建議,以解決這...謝謝我無法將asp.net(應用程序頁面)人員編輯器控制值保存在SharePoint 2013中的人員選擇器字段

回答

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

ConvertLoginAccount函數將解決您的問題。我分享了爲我工作的所有代碼。 –

+0

謝謝CIAODO –

+0

它解決了你的問題嗎? –

相關問題