Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

share|improve this question

2 Answers 2

Please, try the following code:

SPUser user = web.EnsureUser("{user login}");
item["Manager"] = user;
item.Update();
share|improve this answer
    
I tried your sample, but I don't know the user login because the end-user has to type it... I replace your {"user login"} by userValue, definied as follow : SPFieldUserValue userValue = new SPFieldUserValue(web, ufResponsable.ItemId, ufResponsable.Value.ToString()); but it didn't work and my <SharePoint:FormField> still doesn't appear on my webpage... Any other idea ? –  AlexB May 3 '13 at 7:16
    
What type is "ufResponsable"? –  Andrew May 3 '13 at 8:22
up vote 0 down vote accepted

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.