How to display and get the value chosen in an User field?
-
10-12-2019 - |
Question
I'm very new at Sharepoint and I faced to a trick issue.
Here is the situation : in an .aspx.cs file, I've created an EventHandler called btnValidate_Click, raised on a click on a button. In this EventHandler, I need to get values of some fields displayed on the page, in order to insert them in various lists.
In one of my list, I have a User column, and I would like to display it, get the value defined by the end-user, and insert it in my list.
Here is my code :
<table>
<asp:Repeater runat="server" ID="RepeaterWorkTeamUser" DataSourceID="SPDataSourceWorkTeam" EnableViewState="true">
<HeaderTemplate>
<tr>
<%-- Headers --%>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:TextBox runat="server" ID="txtTitle" EnableViewState="true" />
</td>
<td>
<%--Some others fields --%>
</td>
<td>
<SharePoint:UserField runat="server" ID="spufManager" EnableViewState="true" ControlMode="Edit" Visible="true" />
</td>
<td>
</td>
</ItemTemplate>
</table>
In my btnValidate_Click EventHandler, this is how I get my values :
public void btnValidate_Click(object sender, EventArgs e)
{
SPWeb web = SPContext.Current.Web;
SPList listWorkers = web.Lists["WorkTeam"];
foreach (RepeaterItem i in RepeaterWorkTeamUser.Items)
{
SPListItem workTeamListItem = listWorkers.AddItem();
TextBox txtNom = (TextBox)i.FindControl("txtName");
workTeamListItem["Nom"] = txtNom.Text;
<%-- Some others properties --%>
SPFieldUserValue userValue = new SPFieldUserValue(web, web.CurrentUser.ID, web.CurrentUser.LoginName);
workTeamListItem["Manager"] = userValue;
workTeamListItem.Update();
}
But the field "Manager" doesn't even appear on my page, whereas others fields are correctly displayed, and their values are correctly inserted on the list.
I tried with a
Can somebody help me to display it correctly, and get the chosen user in my EventHandler to add it to my list ?
Thanks in advance :-)
Alex
Solution 2
I finally found a solution.
I'm still unable to display a , so I used a PeopleEditor as explained here : http://karinebosch.wordpress.com/sharepoint-controls/peopleeditor-control/
Then, in my aspx.cs file, here is how I get the selected user :
PeopleEditor peManager = (PeopleEditor)i.FindControl("peManager");
peResponsable.AutoPostBack = true;
PickerEntity pe = (PickerEntity)peManager.Entities[0];
workTeamListItem["Manager"] = pe.EntityData["SPUserID"];
Fix it !
Oh, by the way If you experience an issue with IE9, like "Namespace prefix 'xsd' is not defined" when submitting datas, follow this link and look like the M_Olson's answer : his solution perfectly works. http://social.msdn.microsoft.com/forums/en-US/sharepointgeneralprevious/thread/90b3835c-6754-4bb6-9fd4-b74f16f236ff I also use this other M_Olson's answer for this IE9 issue : http://*social.msdn.microsoft.com/forums/en-US/sharepointcustomizationprevious/thread/8457c2f7-70f7-4500-a71c-31b1cf2b22eb
OTHER TIPS
Please, try the following code:
SPUser user = web.EnsureUser("{user login}");
item["Manager"] = user;
item.Update();