Pull e-mail from people picker and write to a separate textfield

18.09.2011 I have posted a new solution to this “problem”. I recommend you use the new one. You find it here


I got this request from Indra:

Hi Alex,

This is really great blog!! awesome work and i am big fan of this site, i check everyday and look at the things and learn , but i need something that i cannot find any where. Let me explain what i need..

I have a list with a people picker and a field called email address, i want to get the email address populated automatically depending the person on people picker ( single user). I know that i need to get user information from profile, but i am not a programmer and there i need your help.

I know you are really busy but please spotlight when you have time.

Thanks in Advance.

This code fills the e-mail address when the item is saved. It pulls the email address from a “resolved” user in a people picker, and writes it to a separate text field.

As always we start like this:
Create a document library to hold your scripts (or a folder on the root created in SharePoint Designer). In this example i have made a document library with a relative URL of “/test/English/Javascript” (a sub site named “test” with a sub site named “English” with a document library named “Javascript”):
IMG

The jQuery-library is found here. The pictures and the sourcecode refers to jquery-1.4.min. If you use another version, please update the reference in the code.

The scripts “interaction.js” and “stringBuffer.js” is created by Erucy and published on CodePlex.

Read here how to add a CEWP to NewForm or EditForm, and how to get the guid for a list.

Add this code to a CEWP below the NewForm or EditForm:
[javascript]
<script type="text/javascript" src="/test/English/Javascript/jquery-1.4.min.js"></script>
<script type="text/javascript" src="/test/English/Javascript/interaction.js"></script>
<script type="text/javascript" src="/test/English/Javascript/stringBuffer.js"></script>
<script type="text/javascript">
fields = init_fields();

userListGuid = ‘570D772F-0EAB-45A8-8C54-9CCD4EC6A0AF’;
userListBaseUrl = ”;
myPeoplePickerFin = ‘MyPeoplePicker’;
myTextFieldFin = ‘MyTextField’;

function PreSaveAction(){
var myPeoplePicker = $(fields[myPeoplePickerFin]).find(‘.ms-formbody’).find("div[id=’divEntityData’]");
if(myPeoplePicker.length>0){
var isResolved = (myPeoplePicker.attr(‘isresolved’).toLowerCase()==’true’)?true:false;
if(isResolved){
var login = myPeoplePicker.attr(‘description’);
var uiObj = getUserInfoFromLogin(login);
$(fields[myTextFieldFin]).find(‘input’).val(uiObj.EMail);
}
}
return true;
}

function getUserInfoFromLogin(UserLogin){ //
wsBaseUrl = userListBaseUrl + ‘/_vti_bin/’;
var retObj = {};
var arrOfFields = [‘ID’,’Name’,’Title’,’EMail’,’Department’,’JobTitle’,’Notes’,’Picture’,’IsSiteAdmin’,
‘Created’,’Author’,’Modified’,’Editor’,’SipAddress’,’Deleted’];

var query = "<Where><Eq><FieldRef Name=’Name’ /><Value Type=’Text’>" + UserLogin + "</Value></Eq></Where>";
var res = queryItems(userListGuid,query,arrOfFields,1);
if(res.count>0){
for(i=0;i<arrOfFields.length;i++){
retObj[arrOfFields[i]] = res.items[0][arrOfFields[i]];
}
return retObj;
}else{
for(i=0;i<arrOfFields.length;i++){
retObj[arrOfFields[i]] = "User with id " + UserLogin + " not found.";
}
return retObj;
}
}

function init_fields(){
var res = {};
$("td.ms-formbody").each(function(){
if($(this).html().indexOf(‘FieldInternalName="’)<0) return;
var start = $(this).html().indexOf(‘FieldInternalName="’)+19;
var stopp = $(this).html().indexOf(‘FieldType="’)-7;
var nm = $(this).html().substring(start,stopp);
res[nm] = this.parentNode;
});
return res;
}
</script>
[/javascript]
You must change these four parameters in the script:

  • userListGuid: The listGuid of the user list
  • userListBaseUrl: The base URL for the userList. Most likely an empty string “”, but If the site resides on a managed path, this must be reflected.
  • myPeoplePickerFin: FieldInternalName for the people picker
  • myTextFieldFin: FieldInternalName for the single line text field to hold the email address

Ask if anything is unclear.

Regards
Alexander

23 thoughts on “Pull e-mail from people picker and write to a separate textfield”

  1. Actually I was using jquery-1.4.2.min.js. That was the undefined error.

    However I’m having trouble getting this to work. When debugging in IE I noticed that the length of the PeoplePicker field is always 0 so I don’t execute the if(myPeoplePicker.length>0) logic in the PreSaveAction() function.

    Any idea why would my pp length is always 0? My parameters are setup up this way:
    userListGuid = ‘0BC378E4-61EF-4F1D-86DD-9DE9D64D4A59’;
    userListBaseUrl = ‘http://portalesdevsrv/development/sites/directoriotelefonico’;
    myPeoplePickerFin = ‘Nombre_’;
    myTextFieldFin = ‘Titulo’;

    Many Thanks!

  2. Yes. That was it. I had the CEWP above the form. Finally making progress. Thanks!

    Now… no matter what user I select I get the following written to my text field: User with id domainusername not found. Profile services seem to be working fine.

    1. Hi,
      Are your site collection in a managed path? if so, you must modify the parameter”userListBaseUrl” to reflect this.

      Alexander

  3. What if I have a site collection under the managed path and a subsite under the site collection?

    Should I say:
    userListBaseUrl = “/ThisIsTheManagedPath/SiteCollectionName/SubSiteName”;

    I tried this but didn’t work.

    Thanks!

    1. The “People and groups” list is located at the root of your site collection. Any subsite must refer this list from the root.

      Go to the root of your site collection > go to the “People and groups” list > right click > view source > search for L_Menu_BaseUrl.

      The value for L_Menu_BaseUrl is your managed path “root”.

      Alexander

  4. This worked perfectly! Thanks!

    Small issue I found:
    Some users get validated in the people picker but still get error:
    User with id domainusername not found in the text field.

    I found that these users weren’t added directly to the user list, instead they got access because of the domain users group. Is this the expected behavior? Any workarounds to avoid the need to add each user to the list (I have about 4000 users for that site)?

    Thanks again.

    1. Hi,
      Does this happen with users that have not yet logged in to the SharePoint site?

      I have not tested it, but i would think that could be the issue. If so, i have no fix for it as the information is pulled from the “user list” in the SharePoint site, and this is not populated before the user logs in the first time.

      Alexander

  5. userListGuid = ‘046EA598-CBA3-47C7-BA53-29120ED4033E’;
    userListBaseUrl = ‘/gscticketing’;
    myPeoplePickerFin = ‘RequestorName’;
    myTextFieldFin = ‘RequestorEmail’;

    I have my page as: http://intraneturl:82/gscticketing/default_ERIC_AD_w_lookup.aspx

    My page uses a data view web part.

    Nothing happens when I edit the requestor name and check the name.

    My fields were renamed to Requestor Name and Requestor Email, but the internal name is still RequestorName and RequestorEmail.

    The guid I am using is from the Members list of my site.

    1. Hi,
      Does the site reside in a managed path? If so, check the “userListBaseUrl”

      The base URL for the userList. Most likely an empty string “”, but If the site resides on a managed path, this must be reflected.

      Alexander

  6. Still wont work. I checked my base URL and it is /gscticketing, I tried it without as well. I am using a CEWP below my default edit form using the browser only and not SPD.

    My GUID is pointing to the Members Permissions Group list of my site. My field names are internal and were changed to Requestor Email and Requestor Name, with spaces, but the interanal names are RequestorEmail and RequestorName.

    I have all javascript files in place. I tried your debug function and got “function” and “undefinded” as a response.

    All Javascript files are in the local List/Forms directory.

    I am a site collection administrator. I can see the list. My name is in the list. I fill in my name as requestor name and it does not populate my email below.

    Please help.

    1. Hi,
      Not sure if this is relevant, but you know that the email is pulled when the item is saved (pressing the OK button) and not when the name is resolved by pressing the “Resolve button”?

      Alexander

  7. I need exactly what you have done but it has to fire in the UI not on save. Can this code be tweaked to run in the UI. For example, after the person is selected from the people picker, and onchange event or listnered would fire and pre populate the new.aspx form. I need this for the OOTB contacts list.

  8. Hello,

    How would you change the above code to have the myTextFieldFin field updated with the email not at item’s save, but at each people picker change [ event .change() ] ?
    I tried numerous possibilities with no success.

    Marc

Leave a Reply