0

I have one People Picker and Label on a page, and once I ensure that the user is available in Active Directory, I need to bind the user's email address to the label control. Where would the code for this need to be written? Should it be in the PageLoad() event handler?

Ryan Shripat
  • 5,574
  • 6
  • 49
  • 77
Ayyappan Anbalagan
  • 11,022
  • 17
  • 64
  • 94

2 Answers2

2

yes, you can access the SPUser object (which holds the email property) like this:

var accountName = peoplePicker.Accounts[0];

//this will create a new account on SharePoint if a user with the given accountName does not exist
var user = web.EnsureUser(accountName); 

lblEmail = user.Email;

peoplePicker obviously is the people picker control, web is an instance of the current web you are in (you can use the web of the SPContext.Current.Web aswell).

There is not specific event that fires when you enter a username in the people picker and hit enter, however you can set the AutoPostback property to true that then fires a generic postback which you can handle via Page_Load...

Define the PeoplePicker in your markup as followed:

<SharePoint:PeopleEditor AutoPostBack="true" ID="peUser" runat="server" />

In the Page_Load you simply check whether the people picker holds one (or more, depends) accounts with the Accounts property and then perform your task...

hope this helps

int32
  • 1,687
  • 1
  • 10
  • 6
  • thanks, where do i need to write this code.once the the user ensured it should populate the email in another label. i dont have any event to handle it, once the user ensured it should happen. – Ayyappan Anbalagan Nov 17 '11 at 08:56
  • if (Page.IsPostBack) { if (peopleEditor.IsValid) { for (int i = 0; i < peopleEditor.ResolvedEntities.Count; i++) { PickerEntity picker = (PickerEntity)peopleEditor.ResolvedEntities[i]; Hashtable hstEntityData = picker.EntityData; string emailID = Convert.ToString(hstEntityData["Email"]); I tried like this but the people picker returning null. – Ayyappan Anbalagan Nov 17 '11 at 10:39
  • Hi Andreas Scharf, I find that if we add the peoplepicker field dynamically its not possible to handle in postback. then i have added the field in .aspx then the above code works great. – Ayyappan Anbalagan Nov 17 '11 at 15:28
  • One problem I found when trying to integrate this code into my own for a similar task - `accountName` must be identified as a string variable, so you have to add `.ToString()` on the end of `Accounts[0]`, so that it can be passed into `.EnsureUser()`. – vapcguy Apr 27 '15 at 21:09
  • yes exactly, accountName has to be of type string. however, peoplePicker.Accounts should return you an ArrayList of type string anyway so there's no need to call an additional .ToString(). glad it works for you :) – int32 Apr 28 '15 at 10:03
0

If all you want is the e-mail address, this should work:

if (pectrl.ResolvedEntities.Count > 0)
{
    PickerEntity pe = (PickerEntity)pectrl.ResolvedEntities[0];
    string email = pe.EntityData[PeopleEditorEntityDataKeys.Email].ToString();
}
vapcguy
  • 7,097
  • 1
  • 56
  • 52