I have created person or group column in sharepoint 2013 list and I have created asp.net application page in vs environment for sharepoint 2013..Problem is I have put people editor control in apllication page when I was attempting to save the people editor value to sharepoint person or group(people picker field)... I got error like String to SPUser convertion is not possible...could you please give suggestions to resolve this...Thanks
0
votes
1 Answers
0
votes
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;
}