Question

I'm Trying to create a new formulary in a new Asp page on my sharepoint site.

So i need a field that can search all users and select one.

I Saw on several forums that the best controls are PeoplePicker or PeopleEditor.

Now I have to get the value from the control and instert it to a list...

Is Javascript the only way to do that? I can't see any good options in the properties...


In my aspx code, I tried to put a userfield control but the page doesn't show it...same for peoplepickerdialog

here's my code:

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
User: <SharePoint:PeopleEditor runat="server" ID="UserPe" /><br />
Start Date: <SharePoint:DateTimeControl runat="server" ID="StartDate" DateOnly="true" /><br />
End Date: <SharePoint:DateTimeControl runat="server" ID="EndDate" DateOnly="true" /><br />
Comment:  <asp:TextBox runat="server" ID="CommentTb" TextMode="MultiLine" /><br />
<asp:Button runat="server" ID="SubmitBtn" Text="Submit" OnClick="SubmitBtn_Click" />
<asp:Button runat="server" ID="CancelBtn" Text="Cancel" OnClick="CancelBtn_Click" />
</asp:Content>

and here's the code behind

protected void SubmitBtn_Click(object sender, EventArgs e)
    {
        SPList thisList = SPContext.Current.Web.Lists["Recuperations"];
        SPListItem newItem = thisList.Items.Add();

        newItem["User"] = GetUser();
        newItem["Start Date"] = StartDate.SelectedDate.Date;
        newItem["End Date"] = EndDate.SelectedDate.Date;
        newItem["Comment"] = CommentTb.Text;

        newItem.Update();

        Context.Response.Redirect(SPContext.Current.Web.Url);
    }

The GetUser() just return an SPUser...it should be working...or not...

Was it helpful?

Solution

This is what I have been using to get an SPFieldUserValue from the resolved users and groups in a PeopleEditor control.

public static SPFieldUserValue GetPeopleFromPickerControl(PeopleEditor people, SPWeb web)
{
    SPFieldUserValue value = null;
    if (people.ResolvedEntities.Count > 0)
    {
        for (int i = 0; i < people.ResolvedEntities.Count; i++)
        {
            try
            {
                PickerEntity user = (PickerEntity)people.ResolvedEntities[i];

                switch ((string)user.EntityData["PrincipalType"])
                {
                    case "User":
                        SPUser webUser = web.EnsureUser(user.Key);
                        value = new SPFieldUserValue(web, webUser.ID, webUser.Name);
                        break;

                    case "SharePointGroup":
                        SPGroup siteGroup = web.SiteGroups[user.EntityData["AccountName"].ToString()];
                        value = new SPFieldUserValue(web, siteGroup.ID, siteGroup.Name);
                        break;
                    default:
                        SPUser spUser = web.EnsureUser(people.Accounts[i].ToString());
                        value = new SPFieldUserValue(web, spUser.ID, spUser.Name);
                        break;
                }
            }
            catch (Exception ex)
            {
                // log or do something
                SPUser spUser = web.EnsureUser(people.Accounts[i].ToString());
                value = new SPFieldUserValue(web, spUser.ID, spUser.Name);
            }
        }
    }
    return value;
}

OTHER TIPS

In the code behind of your aspx use can use the following code:

foreach (PickerEntity resolvedEntity in peopleEditor.ResolvedEntities)
{
    SPPrincipalType principalType = (SPPrincipalType)Enum.Parse(typeof(SPPrincipalType), resolvedEntity.EntityData["PrincipalType"].ToString());
    if (principalType == SPPrincipalType.User || principalType == SPPrincipalType.SecurityGroup)
    {
        string loginName = resolvedEntity.Key;
        //get the user form web.SiteUsers
    }
    else if (principalType == SPPrincipalType.SharePointGroup)
    {
        string groupName = resolvedEntity.Key;
        //get the group here from web.SiteGroups
    }
}

I would revise it like this, where pectrl is the PeopleEditor. Note: This assumes you add a MultiSelect="false" attribute on your PeopleEditor, though theoretically you should be able to use a loop like in Sean's example to get them all, but you would need to return a collection, not just SPUser or SPFieldUserValue to do that:

public static SPUser GetUserFromPickerControl()
{
    SPUser user = null;
    if (pectrl.ResolvedEntities.Count > 0)
    {
        try
        {
            PickerEntity pe = (PickerEntity)pectrl.ResolvedEntities[0];
            SPFieldUserValue userValue = new SPFieldUserValue(web, Convert.ToInt32(pe.EntityData["SPUserID"]), pe.Key);
            user = userValue.User;
        }
        catch (Exception ex)
        {
            // log or do something
        }
    }

    return user;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top