2

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...

5

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;
}
  • How are you calling it? Thanks. – SearchForKnowledge Mar 31 '15 at 13:37
  • 1
    if you have a PeopleEditor control, you would just call this, passing in that PeopleEditor and the SPWeb for which you want your SPUser (or group.) ie from codebehind of an aspx page, if I have a PeopleEditor with the ID "myPE", I might call it like this: GetPeopleFromPickerControl(myPE, SPContext.Current.Web); – Sean Mar 31 '15 at 19:55
  • The best overloaded method match for 'string.this[int]' has some invalid arguments on user.EntityData["PrincipalType"]. – vapcguy Apr 27 '15 at 21:58
  • user.EntityData should be a hashtable - msdn.microsoft.com/en-us/library/… , but according to your error above, it seems your code is treating EntityData as a string, and if it were a string, the only proper way to index into it would be using an int, not a string. I am not quite sure how to assist you with this. – Sean May 4 '15 at 19:54
0

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
    }
}
  • got an error this way – n0r1s Dec 4 '13 at 13:54
  • can't i simply use the "accounts" property? – n0r1s Dec 4 '13 at 14:03
  • What is the error saying? – Naim Murati Dec 4 '13 at 14:05
  • haha you know...sharepoint error...something went wrong XD – n0r1s Dec 4 '13 at 14:09
  • peopleEditor.Account returns ArreyList of strings while ResolvedEntities returns ArreyList of PickerEntity objects with more properties. Anyhow both should return something if there are users in the PeopleEditor. For more error details you can disable customErrors in web.config, or check ULS and Event Viewer. – Naim Murati Dec 4 '13 at 15:01
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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